Skip to content

Commit 2f14a1d

Browse files
committed
Add MCPAgent
1 parent 9c11a73 commit 2f14a1d

File tree

7 files changed

+193
-2
lines changed

7 files changed

+193
-2
lines changed

coagent/agents/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# ruff: noqa: F401
22
from .chat_agent import ChatAgent, confirm, submit, RunContext, StreamChatAgent, tool
33
from .dynamic_triage import DynamicTriage
4+
from .mcp_agent import MCPAgent
45
from .messages import ChatHistory, ChatMessage
56
from .model_client import ModelClient
67
from .parallel import Aggregator, AggregationResult, Parallel

coagent/agents/aswarm/util.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ def greet(
131131
Then you will get a JSON schema with per-parameter descriptions.
132132
"""
133133

134+
if hasattr(func, "__mcp_tool_schema__"):
135+
# If the function already has a schema, return it.
136+
# This is the case for tools used in MCPAgent.
137+
return dict(
138+
type="function",
139+
function=func.__mcp_tool_schema__
140+
)
141+
134142
# Construct the pydantic mdoel for the _under_fn's function signature parameters.
135143
# 1. Get the function signature.
136144

coagent/agents/chat_agent.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@ def system(self) -> str:
234234
def client(self) -> ModelClient:
235235
return self._client
236236

237+
async def get_swarm_agent(self) -> SwarmAgent:
238+
return self._swarm_agent
239+
237240
async def agent(self, agent_type: str) -> AsyncIterator[ChatMessage]:
238241
"""The candidate agent to delegate the conversation to."""
239242
async for chunk in StreamDelegate(self, agent_type).handle(self._history):
@@ -265,8 +268,10 @@ async def _handle_history(
265268
await self.update_user_confirmed(msg)
266269
await self.update_user_submitted(msg)
267270

271+
swarm_agent = await self.get_swarm_agent()
272+
268273
response = self._swarm_client.run_and_stream(
269-
agent=self._swarm_agent,
274+
agent=swarm_agent,
270275
messages=[m.model_dump() for m in msg.messages],
271276
context_variables=msg.extensions,
272277
)

coagent/core/util.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ def direct_values(self, prefix: str) -> list[Any]:
4343

4444

4545
def get_func_args(func) -> set[str]:
46+
if hasattr(func, "__mcp_tool_args__"):
47+
return set(func.__mcp_tool_args__)
48+
4649
hints = get_type_hints(func)
4750
hints.pop("return", None) # Ignore the return type.
4851
return set(hints.keys())

poetry.lock

Lines changed: 101 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ blinker = "1.9.0"
3030
loguru = "0.7.3"
3131
jq = "1.8.0"
3232
litellm = "1.55.12"
33+
mcp = "1.2.0"
3334

3435
[tool.pyright]
3536
# https://github.com/microsoft/pyright/blob/main/docs/configuration.md

uv.lock

Lines changed: 73 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)