Skip to content

Commit bd822c1

Browse files
committed
Add an example
1 parent dc753f2 commit bd822c1

File tree

4 files changed

+143
-0
lines changed

4 files changed

+143
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,7 @@ reporter = AgentSpec(
566566

567567
- [patterns](examples/patterns)
568568
- [a2a](examples/a2a)
569+
- [react-mcp](examples/react-mcp)
569570
- [mcp](examples/mcp)
570571
- [mcp-new](examples/mcp-new)
571572
- [structured-outputs](examples/structured-outputs)

examples/react-mcp/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Model Context Protocol
2+
3+
This example demonstrates how to use [MCP][1] tools in a ReAct agent.
4+
5+
6+
## Prerequisites
7+
8+
- Install `coagent` (see [Installation](../../README.md#installation)).
9+
- Start a NATS server (see [Distributed](../../README.md#distributed)).
10+
11+
12+
## Quick Start
13+
14+
Set the environment variables for your model:
15+
16+
```bash
17+
export MODEL_ID="your-model-id"
18+
export MODEL_BASE_URL="your-base-url"
19+
export MODEL_API_KEY="your-api-key"
20+
```
21+
22+
Run the MCP server:
23+
24+
```bash
25+
python examples/react-mcp/server.py
26+
```
27+
28+
Run the agent:
29+
30+
```bash
31+
python examples/react-mcp/agent.py
32+
```
33+
34+
```
35+
I'll check the weather in Beijing for you right away.
36+
[tool#call_hs9xiswsunbcynv946n43z46 call: get_weather]
37+
38+
[tool#call_hs9xiswsunbcynv946n43z46 progress: Getting weather for Beijing]
39+
40+
[tool#call_hs9xiswsunbcynv946n43z46 output: The weather in Beijing is sunny.]
41+
The weather in Beijing is sunny today! It looks like a beautiful day there.
42+
```
43+
44+
45+
[1]: https://modelcontextprotocol.io/

examples/react-mcp/agent.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import asyncio
2+
from coagent.agents.react_agent import (
3+
ReActAgent,
4+
InputMessage,
5+
InputHistory,
6+
OutputMessage,
7+
MessageOutputItem,
8+
ToolCallItem,
9+
ToolCallOutputItem,
10+
ToolCallProgressItem,
11+
)
12+
from coagent.core import AgentSpec, new, init_logger
13+
from coagent.runtimes import LocalRuntime
14+
import mcputil
15+
16+
17+
async def main():
18+
async with LocalRuntime() as runtime:
19+
# Alternative usage:
20+
# ```
21+
# mcp_client = mcputil.Client(mcputil.StreamableHTTP(url="http://localhost:8000"))
22+
# await mcp_client.connect()
23+
# ...
24+
# await mcp_client.close()
25+
# ```
26+
async with mcputil.Client(
27+
mcputil.StreamableHTTP(url="http://localhost:8000")
28+
) as mcp_client:
29+
reporter = AgentSpec(
30+
"reporter",
31+
new(
32+
ReActAgent,
33+
name="reporter",
34+
system="You are a helpful weather reporter",
35+
tools=[mcp_client],
36+
),
37+
)
38+
await runtime.register(reporter)
39+
40+
result = await reporter.run(
41+
InputHistory(
42+
messages=[
43+
InputMessage(
44+
role="user", content="What's the weather like in Beijing?"
45+
)
46+
]
47+
).encode(),
48+
stream=True,
49+
)
50+
async for chunk in result:
51+
msg = OutputMessage.decode(chunk)
52+
i = msg.item
53+
match i:
54+
case MessageOutputItem():
55+
print(i.raw_item.content[0].text, end="", flush=True)
56+
case ToolCallItem():
57+
print(
58+
f"\n[tool#{i.raw_item.call_id} call: {i.raw_item.name}]",
59+
flush=True,
60+
)
61+
case ToolCallProgressItem():
62+
print(
63+
f"\n[tool#{i.raw_item.call_id} progress: {i.raw_item.message}]",
64+
flush=True,
65+
)
66+
case ToolCallOutputItem():
67+
print(
68+
f"\n[tool#{i.raw_item.call_id} output: {i.raw_item.output}]",
69+
flush=True,
70+
)
71+
72+
73+
if __name__ == "__main__":
74+
init_logger()
75+
asyncio.run(main())

examples/react-mcp/server.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from mcp.server.fastmcp import Context, FastMCP
2+
from mcp.server.session import ServerSession
3+
4+
mcp = FastMCP(name="Progress Example")
5+
6+
7+
@mcp.tool()
8+
async def get_weather(
9+
city: str,
10+
ctx: Context[ServerSession, None],
11+
) -> str:
12+
"""Get the weather for a given city."""
13+
await ctx.report_progress(
14+
progress=0,
15+
total=1.0,
16+
message=f"Getting weather for {city}",
17+
)
18+
return f"The weather in {city} is sunny."
19+
20+
21+
if __name__ == "__main__":
22+
mcp.run(transport="streamable-http")

0 commit comments

Comments
 (0)