-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathagent.py
More file actions
76 lines (71 loc) · 2.44 KB
/
Copy pathagent.py
File metadata and controls
76 lines (71 loc) · 2.44 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
70
71
72
73
74
75
76
import asyncio
from coagent.agents.react_agent import (
ReActAgent,
InputMessage,
InputHistory,
OutputMessage,
MCPTools,
MessageOutputItem,
ToolCallItem,
ToolCallOutputItem,
ToolCallProgressItem,
)
from coagent.core import AgentSpec, new, init_logger
from coagent.runtimes import LocalRuntime
import mcputil
async def main():
async with LocalRuntime() as runtime:
# Alternative usage:
# ```
# mcp_client = mcputil.Client(mcputil.StreamableHTTP(url="http://localhost:8000"))
# await mcp_client.connect()
# ...
# await mcp_client.close()
# ```
async with mcputil.Client(
mcputil.StreamableHTTP(url="http://localhost:8000")
) as mcp_client:
reporter = AgentSpec(
"reporter",
new(
ReActAgent,
name="reporter",
system="You are a helpful weather reporter",
tools=[MCPTools(mcp_client)],
),
)
await runtime.register(reporter)
result = await reporter.run(
InputHistory(
messages=[
InputMessage(
role="user", content="What's the weather like in Beijing?"
)
]
).encode(),
stream=True,
)
async for chunk in result:
msg = OutputMessage.decode(chunk)
i = msg.item
match i:
case MessageOutputItem():
print(i.raw_item.content[0].text, end="", flush=True)
case ToolCallItem():
print(
f"\n[tool#{i.raw_item.call_id} call: {i.raw_item.name}]",
flush=True,
)
case ToolCallProgressItem():
print(
f"\n[tool#{i.raw_item.call_id} progress: {i.raw_item.message}]",
flush=True,
)
case ToolCallOutputItem():
print(
f"\n[tool#{i.raw_item.call_id} output: {i.raw_item.output}]",
flush=True,
)
if __name__ == "__main__":
init_logger()
asyncio.run(main())