75 lines
2.6 KiB
Python
75 lines
2.6 KiB
Python
|
|
from agentdhal_core import Component
|
||
|
|
from pydantic import BaseModel
|
||
|
|
from typing_extensions import Self
|
||
|
|
|
||
|
|
from mcp import ClientSession, Tool
|
||
|
|
|
||
|
|
from ._base import McpToolAdapter
|
||
|
|
from ._config import StdioServerParams
|
||
|
|
|
||
|
|
|
||
|
|
class StdioMcpToolAdapterConfig(BaseModel):
|
||
|
|
"""Configuration for the MCP tool adapter."""
|
||
|
|
|
||
|
|
server_params: StdioServerParams
|
||
|
|
tool: Tool
|
||
|
|
|
||
|
|
|
||
|
|
class StdioMcpToolAdapter(
|
||
|
|
McpToolAdapter[StdioServerParams],
|
||
|
|
Component[StdioMcpToolAdapterConfig],
|
||
|
|
):
|
||
|
|
"""Allows you to wrap an MCP tool running over STDIO and make it available to AutoGen.
|
||
|
|
|
||
|
|
This adapter enables using MCP-compatible tools that communicate over standard input/output
|
||
|
|
with AutoGen agents. Common use cases include wrapping command-line tools and local services
|
||
|
|
that implement the Model Context Protocol (MCP).
|
||
|
|
|
||
|
|
.. note::
|
||
|
|
|
||
|
|
To use this class, you need to install `mcp` extra for the `autogen-ext` package.
|
||
|
|
|
||
|
|
.. code-block:: bash
|
||
|
|
|
||
|
|
pip install -U "agentdhal-ext[mcp]"
|
||
|
|
|
||
|
|
|
||
|
|
Args:
|
||
|
|
server_params (StdioServerParams): Parameters for the MCP server connection,
|
||
|
|
including command to run and its arguments
|
||
|
|
tool (Tool): The MCP tool to wrap
|
||
|
|
session (ClientSession, optional): The MCP client session to use. If not provided,
|
||
|
|
a new session will be created. This is useful for testing or when you want to
|
||
|
|
manage the session lifecycle yourself.
|
||
|
|
|
||
|
|
See :func:`~agentdhal_extensions.tools.mcp.mcp_server_tools` for examples.
|
||
|
|
"""
|
||
|
|
|
||
|
|
component_config_schema = StdioMcpToolAdapterConfig
|
||
|
|
component_provider_override = "agentdhal_extensions.tools.mcp.StdioMcpToolAdapter"
|
||
|
|
|
||
|
|
def __init__(self, server_params: StdioServerParams, tool: Tool, session: ClientSession | None = None) -> None:
|
||
|
|
super().__init__(server_params=server_params, tool=tool, session=session)
|
||
|
|
|
||
|
|
def _to_config(self) -> StdioMcpToolAdapterConfig:
|
||
|
|
"""
|
||
|
|
Convert the adapter to its configuration representation.
|
||
|
|
|
||
|
|
Returns:
|
||
|
|
StdioMcpToolAdapterConfig: The configuration of the adapter.
|
||
|
|
"""
|
||
|
|
return StdioMcpToolAdapterConfig(server_params=self._server_params, tool=self._tool)
|
||
|
|
|
||
|
|
@classmethod
|
||
|
|
def _from_config(cls, config: StdioMcpToolAdapterConfig) -> Self:
|
||
|
|
"""
|
||
|
|
Create an instance of StdioMcpToolAdapter from its configuration.
|
||
|
|
|
||
|
|
Args:
|
||
|
|
config (StdioMcpToolAdapterConfig): The configuration of the adapter.
|
||
|
|
|
||
|
|
Returns:
|
||
|
|
StdioMcpToolAdapter: An instance of StdioMcpToolAdapter.
|
||
|
|
"""
|
||
|
|
return cls(server_params=config.server_params, tool=config.tool)
|