import random
= {
known_weather_data 'berlin': 20.0
}
def get_weather(city: str) -> float:
= city.strip().lower()
city
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
'/Users/tonywu/Documents/scripts/py/zoom_llm/llmzoom_2025/hw3_dlt')
sys.path.append(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
= OpenAI()
client
# === Start the assistant ===
= "You are a helpful assistant. Use tools if necessary."
developer_prompt = ChatInterface()
chat_interface = ChatAssistant(tools, developer_prompt, chat_interface, client)
assistant
assistant.run()
LLM Zoomcamp 2025: dlt Workshop
1 Problem 1
2 Problem 2
def set_weather(city: str, temp: float) -> None:
= city.strip().lower()
city = temp
known_weather_data[city] 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
= OpenAI()
client
# === Start the assistant ===
= "You are a helpful assistant. Use tools if necessary."
developer_prompt = ChatInterface()
chat_interface = ChatAssistant(tools, developer_prompt, chat_interface, client)
assistant
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
= FastMCP("Weather Demo 🌤️")
mcp
# 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.strip().lower()
city
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.strip().lower()
city = temp
known_weather_data[city] 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
= "weather_mcp.py" # adjust if your file name differs
SERVER
async def main():
async with Client(SERVER) as mcp_client:
= await mcp_client.list_tools()
tools
for tool in tools:
# print the tool name and the first line of its description
= tool.description.splitlines()[0] if tool.description else ""
summary 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.