Skip to content

Commit ace7a1e

Browse files
committed
改进注释与字符串并增加对SSE支持的优化处理
1 parent 155084b commit ace7a1e

File tree

2 files changed

+39
-31
lines changed

2 files changed

+39
-31
lines changed

examples/mcp/sse_example/main.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,72 +8,77 @@
88
from agents import Agent, Runner, gen_trace_id, trace
99
from agents.mcp import MCPServer, MCPServerSse
1010
from agents.model_settings import ModelSettings
11+
from examples.models import get_agent_chat_model
1112

1213

1314
async def run(mcp_server: MCPServer):
15+
"""运行代理示例"""
16+
deepseek = get_agent_chat_model("deepseek-v3")
1417
agent = Agent(
15-
name="Assistant",
16-
instructions="Use the tools to answer the questions.",
18+
name="助手",
19+
instructions="使用工具回答问题",
1720
mcp_servers=[mcp_server],
1821
model_settings=ModelSettings(tool_choice="required"),
22+
model=deepseek,
1923
)
2024

21-
# Use the `add` tool to add two numbers
22-
message = "Add these numbers: 7 and 22."
23-
print(f"Running: {message}")
25+
# 使用`add`工具相加两个数字
26+
message = "计算这两个数的和:7和22"
27+
print(f"执行: {message}")
2428
result = await Runner.run(starting_agent=agent, input=message)
2529
print(result.final_output)
2630

27-
# Run the `get_weather` tool
28-
message = "What's the weather in Tokyo?"
29-
print(f"\n\nRunning: {message}")
31+
# 运行`get_weather`工具
32+
message = "东京的天气如何?"
33+
print(f"\n\n执行: {message}")
3034
result = await Runner.run(starting_agent=agent, input=message)
3135
print(result.final_output)
3236

33-
# Run the `get_secret_word` tool
34-
message = "What's the secret word?"
35-
print(f"\n\nRunning: {message}")
37+
# 运行`get_secret_word`工具
38+
message = "秘密单词是什么?"
39+
print(f"\n\n执行: {message}")
3640
result = await Runner.run(starting_agent=agent, input=message)
3741
print(result.final_output)
3842

3943

4044
async def main():
45+
"""主函数"""
4146
async with MCPServerSse(
42-
name="SSE Python Server",
47+
name="SSE Python服务器",
4348
params={
4449
"url": "http://localhost:8000/sse",
4550
},
4651
) as server:
4752
trace_id = gen_trace_id()
48-
with trace(workflow_name="SSE Example", trace_id=trace_id):
49-
print(f"View trace: https://platform.openai.com/traces/trace?trace_id={trace_id}\n")
53+
with trace(workflow_name="SSE示例", trace_id=trace_id):
54+
print(f"查看追踪: https://platform.openai.com/traces/trace?trace_id={trace_id}\n")
5055
await run(server)
5156

5257

5358
if __name__ == "__main__":
54-
# Let's make sure the user has uv installed
59+
# 检查用户是否安装了uv
5560
if not shutil.which("uv"):
5661
raise RuntimeError(
57-
"uv is not installed. Please install it: https://docs.astral.sh/uv/getting-started/installation/"
62+
"未安装uv。请安装: https://docs.astral.sh/uv/getting-started/installation/"
5863
)
5964

60-
# We'll run the SSE server in a subprocess. Usually this would be a remote server, but for this
61-
# demo, we'll run it locally at http://localhost:8000/sse
65+
# 在子进程中运行SSE服务器。通常这会是一个远程服务器,
66+
# 但在这个演示中,我们将在本地运行它:http://localhost:8000/sse
6267
process: subprocess.Popen[Any] | None = None
6368
try:
6469
this_dir = os.path.dirname(os.path.abspath(__file__))
6570
server_file = os.path.join(this_dir, "server.py")
6671

67-
print("Starting SSE server at http://localhost:8000/sse ...")
72+
print("正在启动SSE服务器: http://localhost:8000/sse ...")
6873

69-
# Run `uv run server.py` to start the SSE server
74+
# 运行`uv run server.py`启动SSE服务器
7075
process = subprocess.Popen(["uv", "run", server_file])
71-
# Give it 3 seconds to start
76+
# 等待3秒让它启动
7277
time.sleep(3)
7378

74-
print("SSE server started. Running example...\n\n")
79+
print("SSE服务器已启动。正在运行示例...\n\n")
7580
except Exception as e:
76-
print(f"Error starting SSE server: {e}")
81+
print(f"启动SSE服务器时出错: {e}")
7782
exit(1)
7883

7984
try:

examples/mcp/sse_example/server.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,34 @@
33
import requests
44
from mcp.server.fastmcp import FastMCP
55

6-
# Create server
7-
mcp = FastMCP("Echo Server")
6+
# 创建SSE服务器实例
7+
mcp = FastMCP("回声服务器")
88

99

1010
@mcp.tool()
1111
def add(a: int, b: int) -> int:
12-
"""Add two numbers"""
13-
print(f"[debug-server] add({a}, {b})")
12+
"""两个数字相加"""
13+
print(f"[调试-服务器] add({a}, {b})")
1414
return a + b
1515

1616

1717
@mcp.tool()
1818
def get_secret_word() -> str:
19-
print("[debug-server] get_secret_word()")
20-
return random.choice(["apple", "banana", "cherry"])
19+
"""获取随机秘密单词"""
20+
print("[调试-服务器] get_secret_word()")
21+
return random.choice(["苹果", "香蕉", "樱桃"])
2122

2223

2324
@mcp.tool()
2425
def get_current_weather(city: str) -> str:
25-
print(f"[debug-server] get_current_weather({city})")
26+
"""获取指定城市的当前天气"""
27+
print(f"[调试-服务器] get_current_weather({city})")
2628

2729
endpoint = "https://wttr.in"
28-
response = requests.get(f"{endpoint}/{city}")
30+
response = requests.get(f"{endpoint}/{city}?format=3") # 使用简洁格式
2931
return response.text
3032

3133

3234
if __name__ == "__main__":
35+
# 启动SSE服务器
3336
mcp.run(transport="sse")

0 commit comments

Comments
 (0)