Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions rdagent/components/agent/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from prefect import task
from prefect.cache_policies import INPUTS
from pydantic_ai import Agent
from pydantic_ai.mcp import MCPServerStreamableHTTP

from rdagent.components.agent.mcp_compat import MCPServerHTTP
from rdagent.oai.backend.pydantic_ai import get_agent_model


Expand All @@ -29,7 +29,7 @@ class PAIAgent(BaseAgent):
def __init__(
self,
system_prompt: str,
toolsets: list[str | MCPServerStreamableHTTP],
toolsets: list[str | MCPServerHTTP],
enable_cache: bool = False,
):
"""
Expand All @@ -39,13 +39,13 @@ def __init__(
----------
system_prompt : str
System prompt for the agent
toolsets : list[str | MCPServerStreamableHTTP]
toolsets : list[str | MCPServerHTTP]
List of MCP server URLs or instances
enable_cache : bool
Enable persistent caching via Prefect. Requires Prefect server:
`prefect server start` then set PREFECT_API_URL in environment
"""
toolsets = [(ts if isinstance(ts, MCPServerStreamableHTTP) else MCPServerStreamableHTTP(ts)) for ts in toolsets]
toolsets = [(ts if isinstance(ts, MCPServerHTTP) else MCPServerHTTP(ts)) for ts in toolsets]
self.agent = Agent(get_agent_model(), system_prompt=system_prompt, toolsets=toolsets)
self.enable_cache = enable_cache

Expand Down
6 changes: 3 additions & 3 deletions rdagent/components/agent/context7/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
from typing import Optional

from pydantic_ai.mcp import MCPServerStreamableHTTP

from rdagent.components.agent.base import PAIAgent
from rdagent.components.agent.context7.conf import SETTINGS
from rdagent.log import rdagent_logger as logger
from rdagent.utils.agent.tpl import T

from ..mcp_compat import MCPServerHTTP


class Agent(PAIAgent):
"""
A specific agent for context7
"""

def __init__(self):
toolsets = [MCPServerStreamableHTTP(SETTINGS.url, timeout=SETTINGS.timeout)]
toolsets = [MCPServerHTTP(SETTINGS.url, timeout=SETTINGS.timeout)]

super().__init__(
system_prompt=T(".prompts:system_prompt").r(),
Expand Down
20 changes: 20 additions & 0 deletions rdagent/components/agent/mcp_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""Compatibility shim for the pydantic-ai MCP HTTP server class.

pydantic-ai renamed ``MCPServerStreamableHTTP`` to ``MCPServerHTTP`` during the
1.x line; both names are still exported there, but newer 1.x versions only
document the new name. The 2.x line reworked the MCP API entirely
(``MCPToolsetClient``) and requires a separate migration, so RD-Agent keeps
``pydantic-ai-slim<2`` in ``requirements.txt``.

Importing through this module keeps the agent components working across every
1.x release regardless of which name is available.
"""

from __future__ import annotations

try:
from pydantic_ai.mcp import MCPServerHTTP
except ImportError: # pragma: no cover - only hit on old pydantic-ai 1.x
from pydantic_ai.mcp import MCPServerStreamableHTTP as MCPServerHTTP

__all__ = ["MCPServerHTTP"]
6 changes: 3 additions & 3 deletions rdagent/components/agent/rag/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
from pydantic_ai.mcp import MCPServerStreamableHTTP

from rdagent.components.agent.base import PAIAgent
from rdagent.components.agent.rag.conf import SETTINGS
from rdagent.utils.agent.tpl import T

from ..mcp_compat import MCPServerHTTP


class Agent(PAIAgent):
"""
A specific agent for RAG
"""

def __init__(self, system_prompt: str | None = None):
toolsets = [MCPServerStreamableHTTP(SETTINGS.url, timeout=SETTINGS.timeout)]
toolsets = [MCPServerHTTP(SETTINGS.url, timeout=SETTINGS.timeout)]
if system_prompt is None:
system_prompt = "You are a Retrieval-Augmented Generation (RAG) agent. Use the retrieved documents to answer the user's queries accurately and concisely."
super().__init__(system_prompt=system_prompt, toolsets=toolsets)
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ azureml-mlflow
types-pytz

# Agent
pydantic-ai-slim[mcp,openai,prefect]==1.66.0
pydantic-ai-slim[mcp,openai,prefect]==1.107.4 # keep <2: MCP API reworked in 2.x (see #1432)
nest-asyncio

# visualize SFT train
Expand All @@ -83,4 +83,4 @@ prefect
datasets

# DuckDuckGo search
duckduckgo-search
duckduckgo-search
16 changes: 16 additions & 0 deletions test/utils/test_mcp_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from rdagent.components.agent.mcp_compat import MCPServerHTTP

"""pydantic-ai renamed ``MCPServerStreamableHTTP`` to ``MCPServerHTTP`` in 1.x.

The compat layer must resolve to whichever name the installed pydantic-ai
exposes so agent components keep working across 1.x releases.
"""


def test_importable() -> None:
assert callable(MCPServerHTTP)


def test_construct_with_url() -> None:
server = MCPServerHTTP("http://127.0.0.1:8080", timeout=5)
assert str(server.url) == "http://127.0.0.1:8080"
Loading