import random
known_weather_data = {
'berlin': 20.0
}
def get_weather(city: str) -> float:
city = city.strip().lower()
if city in known_weather_data:
return known_weather_data[city]
return round(random.uniform(-5, 35), 1)
get_weather_tool = {
"type": "function",
"name": "get_weather",
"description": "Input a city and return the weather",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "The city whose weather you want to return"
}
},
"required": ["city"],
"additionalProperties": False
}
}
import sys
sys.path.append('/Users/tonywu/Documents/scripts/py/zoom_llm/llmzoom_2025/hw3_dlt')
from chat_assistant import Tools, ChatInterface, ChatAssistant
tools = Tools()
tools.add_tool(get_weather, get_weather_tool)
# === Define your chat client ===
# Replace this with your actual OpenAI or SDK client
from openai import OpenAI
client = OpenAI()
# === Start the assistant ===
developer_prompt = "You are a helpful assistant. Use tools if necessary."
chat_interface = ChatInterface()
assistant = ChatAssistant(tools, developer_prompt, chat_interface, client)
assistant.run()LLM Zoomcamp 2025: dlt Workshop
1 Problem 1
2 Problem 2
def set_weather(city: str, temp: float) -> None:
city = city.strip().lower()
known_weather_data[city] = temp
return 'OK'
set_weather_tool = {
"type": "function",
"name": "set_weather",
"description": "Input a city and weather to add the entry to known weather data",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "The city whose weather you want to set"
},
"temp": {
"type": "number",
"description": "The temperature of the weather to set"
}
},
"required": ["city", "temp"],
"additionalProperties": False
}
}
tools = Tools()
tools.add_tool(get_weather, get_weather_tool)
tools.add_tool(set_weather, set_weather_tool)
# === Define your chat client ===
# Replace this with your actual OpenAI or SDK client
from openai import OpenAI
client = OpenAI()
# === Start the assistant ===
developer_prompt = "You are a helpful assistant. Use tools if necessary."
chat_interface = ChatInterface()
assistant = ChatAssistant(tools, developer_prompt, chat_interface, client)
assistant.run()3 Problem 3
import fastmcp
print(f"Version: {fastmcp.__version__}")Version: 2.10.5
4 Problem 4
import random
from fastmcp import FastMCP
mcp = FastMCP("Weather Demo 🌤️")
# Shared data store
known_weather_data = {
'berlin': 20.0
}
@mcp.tool
def get_weather(city: str) -> float:
"""
Retrieves the temperature for a specified city.
Parameters:
city (str): The name of the city for which to retrieve weather data.
Returns:
float: The temperature associated with the city.
"""
city = city.strip().lower()
if city in known_weather_data:
return known_weather_data[city]
return round(random.uniform(-5, 35), 1)
@mcp.tool
def set_weather(city: str, temp: float) -> str:
"""
Sets the temperature for a specified city.
Parameters:
city (str): The name of the city for which to set the weather data.
temp (float): The temperature to associate with the city.
Returns:
str: A confirmation string 'OK' indicating successful update.
"""
city = city.strip().lower()
known_weather_data[city] = temp
return 'OK'
if __name__ == "__main__":
mcp.run()The MCP server output prints with transport ‘stdio’
5 Problem 5
{"jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": {"name": "get_weather", "arguments": {"city": "Berlin"}}}
print('{"jsonrpc":"2.0","id":3,"result":{"content":[{"type":"text","text":"20.0"}],"structuredContent":{"result":20.0},"isError":false}}'){"jsonrpc":"2.0","id":3,"result":{"content":[{"type":"text","text":"20.0"}],"structuredContent":{"result":20.0},"isError":false}}
6 Problem 6
# weather_client_pretty.py
from fastmcp import Client
import asyncio
SERVER = "weather_mcp.py" # adjust if your file name differs
async def main():
async with Client(SERVER) as mcp_client:
tools = await mcp_client.list_tools()
for tool in tools:
# print the tool name and the first line of its description
summary = tool.description.splitlines()[0] if tool.description else ""
print(f"{tool.name} – {summary}")
if __name__ == "__main__":
asyncio.run(main())get_weather – Retrieves the temperature for a specified city.
set_weather – Sets the temperature for a specified city.