Repository navigation
Expand file tree
/
Copy pathrun_worker.py
More file actions
62 lines (48 loc) · 1.73 KB
/
Copy pathrun_worker.py
File metadata and controls
62 lines (48 loc) · 1.73 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
"""Worker for the MCP sample. It starts ``echo_mcp_server.py`` on demand."""
# @@@SNIPSTART python-microsoft-agent-framework-mcp-worker
import asyncio
import os
import sys
from pathlib import Path
from agent_framework.openai import OpenAIChatClient
from mcp import Client as MCPClient
from mcp import StdioServerParameters, stdio_client
from openai import AsyncOpenAI
from temporalio.client import Client
from temporalio.microsoft_agent_framework import MicrosoftAgentFrameworkPlugin
from temporalio.worker import Worker
from workflow import MCPWorkflow
ECHO_SERVER = Path(__file__).parent / "echo_mcp_server.py"
def openai_chat_client() -> OpenAIChatClient:
return OpenAIChatClient(
model="gpt-4o-mini", async_client=AsyncOpenAI(max_retries=0)
)
def echo_mcp_client() -> MCPClient:
# Runs on the Worker. The plugin connects on the first MCP Activity and
# keeps the connection for 5 minutes of idleness.
return MCPClient(
stdio_client(
StdioServerParameters(command=sys.executable, args=[str(ECHO_SERVER)])
)
)
async def main() -> None:
if not os.environ.get("OPENAI_API_KEY"):
raise SystemExit("Set OPENAI_API_KEY before starting the Worker.")
plugin = MicrosoftAgentFrameworkPlugin(
chat_clients={"gpt-4o-mini": openai_chat_client},
mcp_clients={"echo": echo_mcp_client},
)
client = await Client.connect(
os.environ.get("TEMPORAL_ADDRESS", "localhost:7233"),
plugins=[plugin],
)
worker = Worker(
client,
task_queue="microsoft-agent-framework-mcp",
workflows=[MCPWorkflow],
)
print("Worker started. Ctrl+C to exit.")
await worker.run()
if __name__ == "__main__":
asyncio.run(main())
# @@@SNIPEND