-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathrun_worker.py
More file actions
54 lines (42 loc) · 1.35 KB
/
Copy pathrun_worker.py
File metadata and controls
54 lines (42 loc) · 1.35 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
"""Worker for the MCP sample.
The worker launches the ``echo_mcp_server.py`` script as a subprocess at
startup. The plugin opens a stdio MCP session, enumerates tools once, and
caches the schema for the worker's lifetime.
"""
# @@@SNIPSTART python-strands-mcp-worker
import asyncio
import os
import sys
from pathlib import Path
from mcp import StdioServerParameters, stdio_client
from strands.tools.mcp.mcp_client import MCPClient
from temporalio.client import Client
from temporalio.contrib.strands import StrandsPlugin
from temporalio.worker import Worker
from strands_plugin.mcp.workflow import MCPWorkflow
ECHO_SERVER = Path(__file__).parent / "echo_mcp_server.py"
def _make_echo_client() -> MCPClient:
return MCPClient(
lambda: stdio_client(
StdioServerParameters(
command=sys.executable,
args=[str(ECHO_SERVER)],
)
)
)
async def main() -> None:
plugin = StrandsPlugin(mcp_clients={"echo": _make_echo_client})
client = await Client.connect(
os.environ.get("TEMPORAL_ADDRESS", "localhost:7233"),
plugins=[plugin],
)
worker = Worker(
client,
task_queue="strands-mcp",
workflows=[MCPWorkflow],
)
print("Worker started. Ctrl+C to exit.")
await worker.run()
if __name__ == "__main__":
asyncio.run(main())
# @@@SNIPEND