-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmcp_server.py
More file actions
61 lines (48 loc) · 1.6 KB
/
mcp_server.py
File metadata and controls
61 lines (48 loc) · 1.6 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
#!/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.
"""Simple MCP server for the graph example (stdio transport).
Provides two tools:
- get_weather: returns mock weather for a location
- calculate: basic arithmetic (add, subtract, multiply, divide)
"""
from mcp.server import FastMCP
app = FastMCP("graph-example-tools")
@app.tool()
async def get_weather(location: str) -> str:
"""Get weather information for a location.
Args:
location: The location name.
Returns:
Weather description string.
"""
weather_data = {
"Beijing": "Sunny, 15°C, humidity 45%",
"Shanghai": "Cloudy, 18°C, humidity 65%",
"Seattle": "Rainy, 12°C, humidity 78%",
}
return weather_data.get(location, f"Weather for {location}: sunny, 20°C")
@app.tool()
async def calculate(operation: str, a: float, b: float) -> float:
"""Perform basic arithmetic.
Args:
operation: One of add, subtract, multiply, divide.
a: First operand.
b: Second operand.
Returns:
Calculation result.
"""
ops = {
"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 y != 0 else float("inf"),
}
if operation not in ops:
raise ValueError(f"Unsupported operation: {operation}")
return ops[operation](a, b)
if __name__ == "__main__":
app.run(transport="stdio")