forked from trpc-group/trpc-agent-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp_server.py
More file actions
69 lines (54 loc) · 1.98 KB
/
Copy pathmcp_server.py
File metadata and controls
69 lines (54 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python3
# Tencent is pleased to support the open source community by making tRPC-Agent-Python available.
#
# Copyright (C) 2026 Tencent. All rights reserved.
#
# tRPC-Agent-Python is licensed under Apache-2.0.
""" MCP server providing weather and calculation tools. """
import logging
from mcp.server import FastMCP
app = FastMCP("simple-tools")
@app.tool()
async def get_weather(location: str) -> str:
"""Get weather information for the specified location.
Args:
location: Name of the location.
Returns:
A string describing the weather.
"""
weather_info = {
"Beijing": "Sunny, 15°C, humidity 45%",
"Shanghai": "Cloudy, 18°C, humidity 65%",
"Shenzhen": "Light rain, 25°C, humidity 80%",
}
return weather_info.get(location, f"Weather data for {location} is not available")
@app.tool()
async def calculate(operation: str, a: float, b: float) -> float:
"""Perform basic math operations.
Args:
operation: Operation type (add, subtract, multiply, divide).
a: First number.
b: Second number.
Returns:
The calculation result.
"""
operations = {
"add": lambda x, y: x + y,
"subtract": lambda x, y: x - y,
"multiply": lambda x, y: x * y,
"divide": lambda x, y: x / y,
}
if operation not in operations:
raise ValueError(f"Unsupported operation: {operation}")
if operation == "divide" and b == 0:
raise ValueError("Division by zero is not allowed")
return operations[operation](a, b)
if __name__ == "__main__":
# Reduce MCP framework logs to error-only to keep demo output clean.
logging.basicConfig(level=logging.ERROR)
logging.getLogger("mcp").setLevel(logging.ERROR)
logging.getLogger("mcp.server").setLevel(logging.ERROR)
# Uncomment ONE of the following lines to select the transport mode:
app.run(transport="stdio")
# app.run(transport="sse")
# app.run(transport="streamable-http")