11import subprocess
22from pathlib import Path
3+ from typing import List
34
4- from .params import MCPServerConfig , MCPServersParams
5+ from agents .mcp import MCPServerStdio , MCPServerStdioParams
6+ from langchain_core .tools import BaseTool
7+ from langchain_mcp_adapters .tools import load_mcp_tools
8+ from autogen_ext .tools .mcp import StdioMcpToolAdapter , StdioServerParams
59
10+ from .exceptions import ServerConfigNotFoundError , SetupError
11+ from .params import MCPServerConfig , MCPServersParams
612
7- class SetupError (Exception ):
8- """Raised when there's an error during server setup."""
9- pass
1013
1114class MCPServers :
1215 def __init__ (self , servers_params : MCPServersParams ):
@@ -120,4 +123,72 @@ def _setup_all_servers(self) -> None:
120123 # Continue with other servers even if one fails
121124 continue
122125
123- print ("Completed server setup process" )
126+ print ("Completed server setup process" )
127+
128+ def make_openai_mcp_server (self , mcp_name : str , cache_tools_list : bool = True ) -> MCPServerStdio :
129+ """
130+ Create and return an OpenAI MCP server for the given MCP name.
131+
132+ Args:
133+ mcp_name: The name of the MCP server configuration to use
134+ cache_tools_list: Whether to cache the tools list (default: True)
135+
136+ Returns:
137+ MCPServerStdio: The configured MCP server
138+
139+ Raises:
140+ ServerConfigNotFoundError: If the server configuration is not found
141+ """
142+ server_config = self .servers_params .retrieve_server_params (mcp_name )
143+ if not server_config :
144+ raise ServerConfigNotFoundError (f"Server configuration not found for '{ mcp_name } '" )
145+
146+ # Convert server config to StdioServerParameters
147+ server_params = MCPServerStdioParams (
148+ command = server_config .command ,
149+ args = server_config .args ,
150+ env = server_config .env ,
151+ cwd = server_config .cwd
152+ )
153+
154+ return MCPServerStdio (
155+ params = server_params ,
156+ cache_tools_list = cache_tools_list
157+ )
158+
159+ async def get_langchain_mcp_tools (self , mcp_name : str , cache_tools_list : bool = True ) -> List [BaseTool ]:
160+ """
161+ Get a list of Langchain tools from an MCP server.
162+
163+ Args:
164+ mcp_name: The name of the MCP server configuration to use
165+ cache_tools_list: Whether to cache the tools list (default: True)
166+
167+ Returns:
168+ List[Tool]: List of Langchain tools provided by the MCP server
169+
170+ Raises:
171+ ServerConfigNotFoundError: If the server configuration is not found
172+ """
173+ async with self .make_openai_mcp_server (mcp_name , cache_tools_list ) as server :
174+ tools = await load_mcp_tools (server .session )
175+ return tools
176+
177+ async def make_autogen_mcp_adapters (self , mcp_name : str ) -> List [StdioMcpToolAdapter ]:
178+ server_config = self .servers_params .retrieve_server_params (mcp_name )
179+ if not server_config :
180+ raise ServerConfigNotFoundError (f"Server configuration not found for '{ mcp_name } '" )
181+
182+ server_params = StdioServerParams (
183+ command = server_config .command ,
184+ args = server_config .args ,
185+ env = server_config .env ,
186+ cwd = server_config .cwd
187+ )
188+
189+ adapters = []
190+ async with self .make_openai_mcp_server (mcp_name , cache_tools_list = True ) as server :
191+ for tool in await server .list_tools ():
192+ adapter = await StdioMcpToolAdapter .from_server_params (server_params , tool .name )
193+ adapters .append (adapter )
194+ return adapters
0 commit comments