Skip to content

Commit 5702304

Browse files
authored
Merge pull request #4 from Cognitive-Stacks/support_frameworks
Support frameworks
2 parents 55bb572 + 73025fc commit 5702304

File tree

6 files changed

+133
-17
lines changed

6 files changed

+133
-17
lines changed

pyproject.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ classifiers = [
2222
"Programming Language :: Python :: 3.12",
2323
]
2424
dependencies = [
25-
"motor>=3.7.0,<4.0.0",
2625
"python-dotenv>=1.0.0,<2.0.0",
2726
"build (>=1.2.2.post1,<2.0.0)",
2827
"twine (>=6.1.0,<7.0.0)",
29-
"pyyaml (>=6.0.2,<7.0.0)",
3028
"openai-agents (>=0.0.9,<0.0.10)",
29+
"langchain-mcp-adapters (>=0.0.7,<0.0.8)",
30+
"autogen-ext[mcp] (>=0.5.1,<0.6.0)",
3131
]
32-
requires-python = ">=3.10"
32+
requires-python = "<4.0,>=3.10"
3333

3434
[project.urls]
3535
Documentation = "https://github.com/yourusername/mcphub#readme"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class ServerConfigNotFoundError(Exception):
2+
"""Raised when a server configuration is not found."""
3+
pass
4+
5+
class SetupError(Exception):
6+
"""Raised when there's an error during server setup."""
7+
pass

src/mcphub/mcp_servers/params.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
from mcp import StdioServerParameters
77

8+
from .exceptions import ServerConfigNotFoundError
9+
810

911
@dataclass
1012
class MCPServerConfig:
@@ -18,10 +20,6 @@ class MCPServerConfig:
1820
setup_script: Optional[str] = None
1921
cwd: Optional[str] = None
2022

21-
class ServerConfigNotFoundError(Exception):
22-
"""Raised when a server configuration is not found."""
23-
pass
24-
2523
class MCPServersParams:
2624
def __init__(self, config_path: str):
2725
self.config_path = config_path

src/mcphub/mcp_servers/servers.py

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import subprocess
22
from 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

1114
class 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

src/mcphub/mcphub.py

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from dataclasses import dataclass, field
22
from pathlib import Path
3-
from typing import Optional
3+
from typing import List, Optional
44

5+
from agents.mcp import MCPServerStdio
6+
from autogen_ext.tools.mcp import StdioMcpToolAdapter
7+
from langchain_core.tools import BaseTool
58
from mcp import StdioServerParameters
6-
7-
from mcp_servers import MCPServerConfig, MCPServersParams, MCPServers
9+
from mcp_servers import MCPServerConfig, MCPServers, MCPServersParams
810

911

1012
@dataclass
@@ -28,4 +30,42 @@ def fetch_server_params(self, mcp_name: str) -> Optional[MCPServerConfig]:
2830
return self.servers_params.retrieve_server_params(mcp_name)
2931

3032
def fetch_stdio_server_config(self, mcp_name: str) -> Optional[StdioServerParameters]:
31-
return self.servers_params.convert_to_stdio_params(mcp_name)
33+
return self.servers_params.convert_to_stdio_params(mcp_name)
34+
35+
def fetch_openai_mcp_server(self, mcp_name: str, cache_tools_list: bool = True) -> MCPServerStdio:
36+
"""
37+
Fetch and return an OpenAI MCP server instance.
38+
39+
Args:
40+
mcp_name: The name of the MCP server to fetch
41+
cache_tools_list: Whether to cache the tools list
42+
43+
Returns:
44+
MCPServerStdio: The configured MCP server
45+
"""
46+
return self.servers.make_openai_mcp_server(mcp_name, cache_tools_list)
47+
48+
async def fetch_langchain_mcp_tools(self, mcp_name: str, cache_tools_list: bool = True) -> List[BaseTool]:
49+
"""
50+
Fetch and return a list of Langchain MCP tools.
51+
52+
Args:
53+
mcp_name: The name of the MCP server to fetch
54+
cache_tools_list: Whether to cache the tools list
55+
56+
Returns:
57+
List[BaseTool]: A list of Langchain MCP tools
58+
"""
59+
return await self.servers.get_langchain_mcp_tools(mcp_name, cache_tools_list)
60+
61+
async def fetch_autogen_mcp_adapters(self, mcp_name: str) -> List[StdioMcpToolAdapter]:
62+
"""
63+
Fetch and return an Autogen MCP adapter.
64+
65+
Args:
66+
mcp_name: The name of the MCP server to fetch
67+
68+
Returns:
69+
StdioMcpToolAdapter: The configured MCP adapter
70+
"""
71+
return await self.servers.make_autogen_mcp_adapters(mcp_name)

src/mcphub/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
# Create an instance of MCPHub, which automatically loads the configuration
88
mcphub = MCPHub()
9-
sequential_thinking_server = mcphub.fetch_server_params("azure-storage-mcp")
9+
sequential_thinking_server = mcphub.fetch_server_params("sequential-thinking-mcp")
1010

1111
print(f"Using MCP server: {sequential_thinking_server}")
1212

0 commit comments

Comments
 (0)