parent
ea66e78154
commit
a612352abd
@ -0,0 +1,83 @@
|
|||||||
|
from swarms import Agent
|
||||||
|
from swarms.tools.mcp_integration import MCPServerSseParams
|
||||||
|
from loguru import logger
|
||||||
|
|
||||||
|
# Comprehensive math prompt that encourages proper JSON formatting
|
||||||
|
MATH_AGENT_PROMPT = """
|
||||||
|
You are a helpful math calculator assistant.
|
||||||
|
|
||||||
|
Your role is to understand natural language math requests and perform calculations.
|
||||||
|
When asked to perform calculations:
|
||||||
|
|
||||||
|
1. Determine the operation (add, multiply, or divide)
|
||||||
|
2. Extract the numbers from the request
|
||||||
|
3. Use the appropriate math operation tool
|
||||||
|
|
||||||
|
FORMAT YOUR TOOL CALLS AS JSON with this format:
|
||||||
|
{"tool_name": "add", "a": <first_number>, "b": <second_number>}
|
||||||
|
or
|
||||||
|
{"tool_name": "multiply", "a": <first_number>, "b": <second_number>}
|
||||||
|
or
|
||||||
|
{"tool_name": "divide", "a": <first_number>, "b": <second_number>}
|
||||||
|
|
||||||
|
Always respond with a tool call in JSON format first, followed by a brief explanation.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def initialize_math_system():
|
||||||
|
"""Initialize the math agent with MCP server configuration."""
|
||||||
|
# Configure the MCP server connection
|
||||||
|
math_server = MCPServerSseParams(
|
||||||
|
url="http://0.0.0.0:8000",
|
||||||
|
headers={"Content-Type": "application/json"},
|
||||||
|
timeout=5.0,
|
||||||
|
sse_read_timeout=30.0
|
||||||
|
)
|
||||||
|
|
||||||
|
# Create the agent with the MCP server configuration
|
||||||
|
math_agent = Agent(
|
||||||
|
agent_name="Math Assistant",
|
||||||
|
agent_description="Friendly math calculator",
|
||||||
|
system_prompt=MATH_AGENT_PROMPT,
|
||||||
|
max_loops=1,
|
||||||
|
mcp_servers=[math_server], # Pass MCP server config as a list
|
||||||
|
model_name="gpt-3.5-turbo",
|
||||||
|
verbose=True # Enable verbose mode to see more details
|
||||||
|
)
|
||||||
|
|
||||||
|
return math_agent
|
||||||
|
|
||||||
|
def main():
|
||||||
|
try:
|
||||||
|
logger.info("Initializing math system...")
|
||||||
|
math_agent = initialize_math_system()
|
||||||
|
|
||||||
|
print("\nMath Calculator Ready!")
|
||||||
|
print("Ask me any math question!")
|
||||||
|
print("Examples: 'what is 5 plus 3?' or 'can you multiply 4 and 6?'")
|
||||||
|
print("Type 'exit' to quit\n")
|
||||||
|
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
query = input("What would you like to calculate? ").strip()
|
||||||
|
if not query:
|
||||||
|
continue
|
||||||
|
if query.lower() == 'exit':
|
||||||
|
break
|
||||||
|
|
||||||
|
logger.info(f"Processing query: {query}")
|
||||||
|
result = math_agent.run(query)
|
||||||
|
print(f"\nResult: {result}\n")
|
||||||
|
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("\nGoodbye!")
|
||||||
|
break
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error processing query: {e}")
|
||||||
|
print(f"Sorry, there was an error: {str(e)}")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"System initialization error: {e}")
|
||||||
|
print(f"Failed to start the math system: {str(e)}")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
@ -1,53 +1,83 @@
|
|||||||
|
from swarms import Agent
|
||||||
|
from swarms.tools.mcp_integration import MCPServerSseParams
|
||||||
|
from loguru import logger
|
||||||
|
|
||||||
from swarms import Agent
|
# Comprehensive math prompt that encourages proper JSON formatting
|
||||||
from swarms.tools.mcp_integration import MCPServerSseParams
|
MATH_AGENT_PROMPT = """
|
||||||
from swarms.prompts.agent_prompts import MATH_AGENT_PROMPT
|
You are a helpful math calculator assistant.
|
||||||
from loguru import logger
|
|
||||||
|
Your role is to understand natural language math requests and perform calculations.
|
||||||
def initialize_math_system():
|
When asked to perform calculations:
|
||||||
"""Initialize the math agent with MCP server configuration."""
|
|
||||||
math_server = MCPServerSseParams(
|
1. Determine the operation (add, multiply, or divide)
|
||||||
url="http://0.0.0.0:8000",
|
2. Extract the numbers from the request
|
||||||
headers={"Content-Type": "application/json"},
|
3. Use the appropriate math operation tool
|
||||||
timeout=5.0,
|
|
||||||
sse_read_timeout=30.0
|
FORMAT YOUR TOOL CALLS AS JSON with this format:
|
||||||
)
|
{"tool_name": "add", "a": <first_number>, "b": <second_number>}
|
||||||
|
or
|
||||||
math_agent = Agent(
|
{"tool_name": "multiply", "a": <first_number>, "b": <second_number>}
|
||||||
agent_name="Math Assistant",
|
or
|
||||||
agent_description="Friendly math calculator",
|
{"tool_name": "divide", "a": <first_number>, "b": <second_number>}
|
||||||
system_prompt=MATH_AGENT_PROMPT,
|
|
||||||
max_loops=1,
|
Always respond with a tool call in JSON format first, followed by a brief explanation.
|
||||||
mcp_servers=[math_server],
|
"""
|
||||||
model_name="gpt-3.5-turbo"
|
|
||||||
)
|
def initialize_math_system():
|
||||||
|
"""Initialize the math agent with MCP server configuration."""
|
||||||
return math_agent
|
# Configure the MCP server connection
|
||||||
|
math_server = MCPServerSseParams(
|
||||||
def main():
|
url="http://0.0.0.0:8000",
|
||||||
math_agent = initialize_math_system()
|
headers={"Content-Type": "application/json"},
|
||||||
|
timeout=5.0,
|
||||||
print("\nMath Calculator Ready!")
|
sse_read_timeout=30.0
|
||||||
print("Ask me any math question!")
|
)
|
||||||
print("Examples: 'what is 5 plus 3?' or 'can you multiply 4 and 6?'")
|
|
||||||
print("Type 'exit' to quit\n")
|
# Create the agent with the MCP server configuration
|
||||||
|
math_agent = Agent(
|
||||||
while True:
|
agent_name="Math Assistant",
|
||||||
try:
|
agent_description="Friendly math calculator",
|
||||||
query = input("What would you like to calculate? ").strip()
|
system_prompt=MATH_AGENT_PROMPT,
|
||||||
if not query:
|
max_loops=1,
|
||||||
continue
|
mcp_servers=[math_server], # Pass MCP server config as a list
|
||||||
if query.lower() == 'exit':
|
model_name="gpt-3.5-turbo",
|
||||||
break
|
verbose=True # Enable verbose mode to see more details
|
||||||
|
)
|
||||||
result = math_agent.run(query)
|
|
||||||
print(f"\nResult: {result}\n")
|
return math_agent
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
def main():
|
||||||
print("\nGoodbye!")
|
try:
|
||||||
break
|
logger.info("Initializing math system...")
|
||||||
except Exception as e:
|
math_agent = initialize_math_system()
|
||||||
logger.error(f"Error: {e}")
|
|
||||||
|
print("\nMath Calculator Ready!")
|
||||||
if __name__ == "__main__":
|
print("Ask me any math question!")
|
||||||
main()
|
print("Examples: 'what is 5 plus 3?' or 'can you multiply 4 and 6?'")
|
||||||
|
print("Type 'exit' to quit\n")
|
||||||
|
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
query = input("What would you like to calculate? ").strip()
|
||||||
|
if not query:
|
||||||
|
continue
|
||||||
|
if query.lower() == 'exit':
|
||||||
|
break
|
||||||
|
|
||||||
|
logger.info(f"Processing query: {query}")
|
||||||
|
result = math_agent.run(query)
|
||||||
|
print(f"\nResult: {result}\n")
|
||||||
|
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("\nGoodbye!")
|
||||||
|
break
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error processing query: {e}")
|
||||||
|
print(f"Sorry, there was an error: {str(e)}")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"System initialization error: {e}")
|
||||||
|
print(f"Failed to start the math system: {str(e)}")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
@ -1,38 +1,79 @@
|
|||||||
|
|
||||||
from fastmcp import FastMCP
|
from fastmcp import FastMCP
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
import time
|
||||||
|
|
||||||
|
# Create the MCP server
|
||||||
|
mcp = FastMCP(host="0.0.0.0",
|
||||||
|
port=8000,
|
||||||
|
transport="sse",
|
||||||
|
require_session_id=False)
|
||||||
|
|
||||||
mcp = FastMCP(
|
|
||||||
host="0.0.0.0",
|
|
||||||
port=8000,
|
|
||||||
transport="sse",
|
|
||||||
require_session_id=False
|
|
||||||
)
|
|
||||||
|
|
||||||
|
# Define tools with proper type hints and docstrings
|
||||||
@mcp.tool()
|
@mcp.tool()
|
||||||
def add(a: int, b: int) -> str:
|
def add(a: int, b: int) -> str:
|
||||||
"""Add two numbers."""
|
"""Add two numbers.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
a (int): First number
|
||||||
|
b (int): Second number
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: A message containing the sum
|
||||||
|
"""
|
||||||
|
logger.info(f"Adding {a} and {b}")
|
||||||
result = a + b
|
result = a + b
|
||||||
return f"The sum of {a} and {b} is {result}"
|
return f"The sum of {a} and {b} is {result}"
|
||||||
|
|
||||||
@mcp.tool()
|
|
||||||
|
@mcp.tool()
|
||||||
def multiply(a: int, b: int) -> str:
|
def multiply(a: int, b: int) -> str:
|
||||||
"""Multiply two numbers."""
|
"""Multiply two numbers.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
a (int): First number
|
||||||
|
b (int): Second number
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: A message containing the product
|
||||||
|
"""
|
||||||
|
logger.info(f"Multiplying {a} and {b}")
|
||||||
result = a * b
|
result = a * b
|
||||||
return f"The product of {a} and {b} is {result}"
|
return f"The product of {a} and {b} is {result}"
|
||||||
|
|
||||||
|
|
||||||
@mcp.tool()
|
@mcp.tool()
|
||||||
def divide(a: int, b: int) -> str:
|
def divide(a: int, b: int) -> str:
|
||||||
"""Divide two numbers."""
|
"""Divide two numbers.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
a (int): Numerator
|
||||||
|
b (int): Denominator
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: A message containing the division result or an error message
|
||||||
|
"""
|
||||||
|
logger.info(f"Dividing {a} by {b}")
|
||||||
if b == 0:
|
if b == 0:
|
||||||
|
logger.warning("Division by zero attempted")
|
||||||
return "Cannot divide by zero"
|
return "Cannot divide by zero"
|
||||||
result = a / b
|
result = a / b
|
||||||
return f"{a} divided by {b} is {result}"
|
return f"{a} divided by {b} is {result}"
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
try:
|
try:
|
||||||
logger.info("Starting math server on http://0.0.0.0:8000")
|
logger.info("Starting math server on http://0.0.0.0:8000")
|
||||||
|
print("Math MCP Server is running. Press Ctrl+C to stop.")
|
||||||
|
|
||||||
|
# Add a small delay to ensure logging is complete before the server starts
|
||||||
|
time.sleep(0.5)
|
||||||
|
|
||||||
|
# Run the MCP server
|
||||||
mcp.run()
|
mcp.run()
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
logger.info("Server shutdown requested")
|
||||||
|
print("\nShutting down server...")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Server error: {e}")
|
logger.error(f"Server error: {e}")
|
||||||
raise
|
raise
|
||||||
|
@ -1,255 +1,320 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import abc
|
import abc
|
||||||
import asyncio
|
import asyncio
|
||||||
from contextlib import AbstractAsyncContextManager, AsyncExitStack
|
from contextlib import AbstractAsyncContextManager, AsyncExitStack
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any, Dict, List, Optional, Literal
|
from typing import Any, Dict, List, Optional, Literal, Union
|
||||||
from typing_extensions import NotRequired, TypedDict
|
from typing_extensions import NotRequired, TypedDict
|
||||||
|
|
||||||
from anyio.streams.memory import MemoryObjectReceiveStream, MemoryObjectSendStream
|
from anyio.streams.memory import MemoryObjectReceiveStream, MemoryObjectSendStream
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
from mcp import ClientSession, StdioServerParameters, Tool as MCPTool, stdio_client
|
from mcp import ClientSession, StdioServerParameters, Tool as MCPTool, stdio_client
|
||||||
from mcp.client.sse import sse_client
|
from mcp.client.sse import sse_client
|
||||||
from mcp.types import CallToolResult, JSONRPCMessage
|
from mcp.types import CallToolResult, JSONRPCMessage
|
||||||
|
|
||||||
from swarms.utils.any_to_str import any_to_str
|
from swarms.utils.any_to_str import any_to_str
|
||||||
|
|
||||||
|
|
||||||
class MCPServer(abc.ABC):
|
class MCPServer(abc.ABC):
|
||||||
"""Base class for Model Context Protocol servers."""
|
"""Base class for Model Context Protocol servers."""
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
async def connect(self) -> None:
|
async def connect(self) -> None:
|
||||||
"""Establish connection to the MCP server."""
|
"""Establish connection to the MCP server."""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
"""Human-readable server name."""
|
"""Human-readable server name."""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
async def cleanup(self) -> None:
|
async def cleanup(self) -> None:
|
||||||
"""Clean up resources and close connection."""
|
"""Clean up resources and close connection."""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
async def list_tools(self) -> List[MCPTool]:
|
async def list_tools(self) -> List[MCPTool]:
|
||||||
"""List available MCP tools on the server."""
|
"""List available MCP tools on the server."""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
async def call_tool(
|
async def call_tool(
|
||||||
self, tool_name: str, arguments: Dict[str, Any] | None
|
self, tool_name: str, arguments: Dict[str, Any] | None
|
||||||
) -> CallToolResult:
|
) -> CallToolResult:
|
||||||
"""Invoke a tool by name with provided arguments."""
|
"""Invoke a tool by name with provided arguments."""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class _MCPServerWithClientSession(MCPServer, abc.ABC):
|
class _MCPServerWithClientSession(MCPServer, abc.ABC):
|
||||||
"""Mixin providing ClientSession-based MCP communication."""
|
"""Mixin providing ClientSession-based MCP communication."""
|
||||||
|
|
||||||
def __init__(self, cache_tools_list: bool = False):
|
def __init__(self, cache_tools_list: bool = False):
|
||||||
self.session: Optional[ClientSession] = None
|
self.session: Optional[ClientSession] = None
|
||||||
self.exit_stack: AsyncExitStack = AsyncExitStack()
|
self.exit_stack: AsyncExitStack = AsyncExitStack()
|
||||||
self._cleanup_lock = asyncio.Lock()
|
self._cleanup_lock = asyncio.Lock()
|
||||||
self.cache_tools_list = cache_tools_list
|
self.cache_tools_list = cache_tools_list
|
||||||
self._cache_dirty = True
|
self._cache_dirty = True
|
||||||
self._tools_list: Optional[List[MCPTool]] = None
|
self._tools_list: Optional[List[MCPTool]] = None
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def create_streams(
|
def create_streams(
|
||||||
self
|
self
|
||||||
) -> AbstractAsyncContextManager[
|
) -> AbstractAsyncContextManager[
|
||||||
tuple[
|
tuple[
|
||||||
MemoryObjectReceiveStream[JSONRPCMessage | Exception],
|
MemoryObjectReceiveStream[JSONRPCMessage | Exception],
|
||||||
MemoryObjectSendStream[JSONRPCMessage],
|
MemoryObjectSendStream[JSONRPCMessage],
|
||||||
]
|
]
|
||||||
]:
|
]:
|
||||||
"""Supply the read/write streams for the MCP transport."""
|
"""Supply the read/write streams for the MCP transport."""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
async def __aenter__(self) -> MCPServer:
|
async def __aenter__(self) -> MCPServer:
|
||||||
await self.connect()
|
await self.connect()
|
||||||
return self # type: ignore
|
return self # type: ignore
|
||||||
|
|
||||||
async def __aexit__(self, exc_type, exc_value, tb) -> None:
|
async def __aexit__(self, exc_type, exc_value, tb) -> None:
|
||||||
await self.cleanup()
|
await self.cleanup()
|
||||||
|
|
||||||
async def connect(self) -> None:
|
async def connect(self) -> None:
|
||||||
"""Initialize transport and ClientSession."""
|
"""Initialize transport and ClientSession."""
|
||||||
try:
|
try:
|
||||||
transport = await self.exit_stack.enter_async_context(
|
transport = await self.exit_stack.enter_async_context(
|
||||||
self.create_streams()
|
self.create_streams()
|
||||||
)
|
)
|
||||||
read, write = transport
|
read, write = transport
|
||||||
session = await self.exit_stack.enter_async_context(
|
session = await self.exit_stack.enter_async_context(
|
||||||
ClientSession(read, write)
|
ClientSession(read, write)
|
||||||
)
|
)
|
||||||
await session.initialize()
|
await session.initialize()
|
||||||
self.session = session
|
self.session = session
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error initializing MCP server: {e}")
|
logger.error(f"Error initializing MCP server: {e}")
|
||||||
await self.cleanup()
|
await self.cleanup()
|
||||||
raise
|
raise
|
||||||
|
|
||||||
async def cleanup(self) -> None:
|
async def cleanup(self) -> None:
|
||||||
"""Close session and transport."""
|
"""Close session and transport."""
|
||||||
async with self._cleanup_lock:
|
async with self._cleanup_lock:
|
||||||
try:
|
try:
|
||||||
await self.exit_stack.aclose()
|
await self.exit_stack.aclose()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error during cleanup: {e}")
|
logger.error(f"Error during cleanup: {e}")
|
||||||
finally:
|
finally:
|
||||||
self.session = None
|
self.session = None
|
||||||
|
|
||||||
async def list_tools(self) -> List[MCPTool]:
|
async def list_tools(self) -> List[MCPTool]:
|
||||||
if not self.session:
|
if not self.session:
|
||||||
raise RuntimeError("Server not connected. Call connect() first.")
|
raise RuntimeError("Server not connected. Call connect() first.")
|
||||||
if self.cache_tools_list and not self._cache_dirty and self._tools_list:
|
if self.cache_tools_list and not self._cache_dirty and self._tools_list:
|
||||||
return self._tools_list
|
return self._tools_list
|
||||||
self._cache_dirty = False
|
self._cache_dirty = False
|
||||||
self._tools_list = (await self.session.list_tools()).tools
|
self._tools_list = (await self.session.list_tools()).tools
|
||||||
return self._tools_list # type: ignore
|
return self._tools_list # type: ignore
|
||||||
|
|
||||||
async def call_tool(
|
async def call_tool(
|
||||||
self, tool_name: str | None = None, arguments: Dict[str, Any] | None = None
|
self, tool_name: str | None = None, arguments: Dict[str, Any] | None = None
|
||||||
) -> CallToolResult:
|
) -> CallToolResult:
|
||||||
if not arguments:
|
if not arguments:
|
||||||
raise ValueError("Arguments dict is required to call a tool")
|
raise ValueError("Arguments dict is required to call a tool")
|
||||||
name = tool_name or arguments.get("tool_name") or arguments.get("name")
|
name = tool_name or arguments.get("tool_name") or arguments.get("name")
|
||||||
if not name:
|
if not name:
|
||||||
raise ValueError("Tool name missing in arguments")
|
raise ValueError("Tool name missing in arguments")
|
||||||
if not self.session:
|
if not self.session:
|
||||||
raise RuntimeError("Server not connected. Call connect() first.")
|
raise RuntimeError("Server not connected. Call connect() first.")
|
||||||
return await self.session.call_tool(name, arguments)
|
return await self.session.call_tool(name, arguments)
|
||||||
|
|
||||||
|
|
||||||
class MCPServerStdioParams(TypedDict):
|
class MCPServerStdioParams(TypedDict):
|
||||||
"""Configuration for stdio transport."""
|
"""Configuration for stdio transport."""
|
||||||
command: str
|
command: str
|
||||||
args: NotRequired[List[str]]
|
args: NotRequired[List[str]]
|
||||||
env: NotRequired[Dict[str, str]]
|
env: NotRequired[Dict[str, str]]
|
||||||
cwd: NotRequired[str | Path]
|
cwd: NotRequired[str | Path]
|
||||||
encoding: NotRequired[str]
|
encoding: NotRequired[str]
|
||||||
encoding_error_handler: NotRequired[Literal["strict", "ignore", "replace"]]
|
encoding_error_handler: NotRequired[Literal["strict", "ignore", "replace"]]
|
||||||
|
|
||||||
|
|
||||||
class MCPServerStdio(_MCPServerWithClientSession):
|
class MCPServerStdio(_MCPServerWithClientSession):
|
||||||
"""MCP server over stdio transport."""
|
"""MCP server over stdio transport."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
params: MCPServerStdioParams,
|
params: MCPServerStdioParams,
|
||||||
cache_tools_list: bool = False,
|
cache_tools_list: bool = False,
|
||||||
name: Optional[str] = None,
|
name: Optional[str] = None,
|
||||||
):
|
):
|
||||||
super().__init__(cache_tools_list)
|
super().__init__(cache_tools_list)
|
||||||
self.params = StdioServerParameters(
|
self.params = StdioServerParameters(
|
||||||
command=params["command"],
|
command=params["command"],
|
||||||
args=params.get("args", []),
|
args=params.get("args", []),
|
||||||
env=params.get("env"),
|
env=params.get("env"),
|
||||||
cwd=params.get("cwd"),
|
cwd=params.get("cwd"),
|
||||||
encoding=params.get("encoding", "utf-8"),
|
encoding=params.get("encoding", "utf-8"),
|
||||||
encoding_error_handler=params.get("encoding_error_handler", "strict"),
|
encoding_error_handler=params.get("encoding_error_handler", "strict"),
|
||||||
)
|
)
|
||||||
self._name = name or f"stdio:{self.params.command}"
|
self._name = name or f"stdio:{self.params.command}"
|
||||||
|
|
||||||
def create_streams(self) -> AbstractAsyncContextManager[
|
def create_streams(self) -> AbstractAsyncContextManager[
|
||||||
tuple[
|
tuple[
|
||||||
MemoryObjectReceiveStream[JSONRPCMessage | Exception],
|
MemoryObjectReceiveStream[JSONRPCMessage | Exception],
|
||||||
MemoryObjectSendStream[JSONRPCMessage],
|
MemoryObjectSendStream[JSONRPCMessage],
|
||||||
]
|
]
|
||||||
]:
|
]:
|
||||||
return stdio_client(self.params)
|
return stdio_client(self.params)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
return self._name
|
return self._name
|
||||||
|
|
||||||
|
|
||||||
class MCPServerSseParams(TypedDict):
|
class MCPServerSseParams(TypedDict):
|
||||||
"""Configuration for HTTP+SSE transport."""
|
"""Configuration for HTTP+SSE transport."""
|
||||||
url: str
|
url: str
|
||||||
headers: NotRequired[Dict[str, str]]
|
headers: NotRequired[Dict[str, str]]
|
||||||
timeout: NotRequired[float]
|
timeout: NotRequired[float]
|
||||||
sse_read_timeout: NotRequired[float]
|
sse_read_timeout: NotRequired[float]
|
||||||
|
|
||||||
|
|
||||||
class MCPServerSse(_MCPServerWithClientSession):
|
class MCPServerSse(_MCPServerWithClientSession):
|
||||||
"""MCP server over HTTP with SSE transport."""
|
"""MCP server over HTTP with SSE transport."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
params: MCPServerSseParams,
|
params: MCPServerSseParams,
|
||||||
cache_tools_list: bool = False,
|
cache_tools_list: bool = False,
|
||||||
name: Optional[str] = None,
|
name: Optional[str] = None,
|
||||||
):
|
):
|
||||||
super().__init__(cache_tools_list)
|
super().__init__(cache_tools_list)
|
||||||
self.params = params
|
self.params = params
|
||||||
self._name = name or f"sse:{params['url']}"
|
self._name = name or f"sse:{params['url']}"
|
||||||
|
|
||||||
def create_streams(self) -> AbstractAsyncContextManager[
|
def create_streams(self) -> AbstractAsyncContextManager[
|
||||||
tuple[
|
tuple[
|
||||||
MemoryObjectReceiveStream[JSONRPCMessage | Exception],
|
MemoryObjectReceiveStream[JSONRPCMessage | Exception],
|
||||||
MemoryObjectSendStream[JSONRPCMessage],
|
MemoryObjectSendStream[JSONRPCMessage],
|
||||||
]
|
]
|
||||||
]:
|
]:
|
||||||
return sse_client(
|
return sse_client(
|
||||||
url=self.params["url"],
|
url=self.params["url"],
|
||||||
headers=self.params.get("headers"),
|
headers=self.params.get("headers"),
|
||||||
timeout=self.params.get("timeout", 5),
|
timeout=self.params.get("timeout", 5),
|
||||||
sse_read_timeout=self.params.get("sse_read_timeout", 300),
|
sse_read_timeout=self.params.get("sse_read_timeout", 300),
|
||||||
)
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
return self._name
|
return self._name
|
||||||
|
|
||||||
|
|
||||||
async def call_tool_fast(
|
async def call_tool_fast(
|
||||||
server: MCPServerSse, payload: Dict[str, Any] | str
|
server: MCPServerSse, payload: Dict[str, Any] | str
|
||||||
) -> Any:
|
) -> Any:
|
||||||
try:
|
"""Async function to call a tool on a server with proper cleanup."""
|
||||||
await server.connect()
|
try:
|
||||||
result = await server.call_tool(arguments=payload if isinstance(payload, dict) else None)
|
await server.connect()
|
||||||
return result
|
arguments = payload if isinstance(payload, dict) else None
|
||||||
finally:
|
result = await server.call_tool(arguments=arguments)
|
||||||
await server.cleanup()
|
return result
|
||||||
|
finally:
|
||||||
|
await server.cleanup()
|
||||||
async def mcp_flow_get_tool_schema(
|
|
||||||
params: MCPServerSseParams,
|
|
||||||
) -> Any:
|
async def mcp_flow_get_tool_schema(
|
||||||
async with MCPServerSse(params) as server:
|
params: MCPServerSseParams,
|
||||||
tools = await server.list_tools()
|
) -> Any:
|
||||||
return tools
|
"""Async function to get tool schema from MCP server."""
|
||||||
|
async with MCPServerSse(params) as server:
|
||||||
|
tools = await server.list_tools()
|
||||||
async def mcp_flow(
|
return tools
|
||||||
params: MCPServerSseParams,
|
|
||||||
function_call: Dict[str, Any] | str,
|
|
||||||
) -> Any:
|
async def mcp_flow(
|
||||||
async with MCPServerSse(params) as server:
|
params: MCPServerSseParams,
|
||||||
return await call_tool_fast(server, function_call)
|
function_call: Dict[str, Any] | str,
|
||||||
|
) -> Any:
|
||||||
|
"""Async function to call a tool with given parameters."""
|
||||||
async def _call_one_server(
|
async with MCPServerSse(params) as server:
|
||||||
params: MCPServerSseParams, payload: Dict[str, Any] | str
|
return await call_tool_fast(server, function_call)
|
||||||
) -> Any:
|
|
||||||
server = MCPServerSse(params)
|
|
||||||
try:
|
async def _call_one_server(
|
||||||
await server.connect()
|
params: MCPServerSseParams, payload: Dict[str, Any] | str
|
||||||
return await server.call_tool(arguments=payload if isinstance(payload, dict) else None)
|
) -> Any:
|
||||||
finally:
|
"""Helper function to call a single MCP server."""
|
||||||
await server.cleanup()
|
server = MCPServerSse(params)
|
||||||
|
try:
|
||||||
|
await server.connect()
|
||||||
def batch_mcp_flow(
|
arguments = payload if isinstance(payload, dict) else None
|
||||||
params: List[MCPServerSseParams], payload: Dict[str, Any] | str
|
return await server.call_tool(arguments=arguments)
|
||||||
) -> List[Any]:
|
finally:
|
||||||
return asyncio.run(
|
await server.cleanup()
|
||||||
asyncio.gather(*[_call_one_server(p, payload) for p in params])
|
|
||||||
)
|
|
||||||
|
async def abatch_mcp_flow(
|
||||||
|
params: List[MCPServerSseParams], payload: Dict[str, Any] | str
|
||||||
|
) -> List[Any]:
|
||||||
|
"""Async function to execute a batch of MCP calls concurrently.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
params (List[MCPServerSseParams]): List of MCP server configurations
|
||||||
|
payload (Dict[str, Any] | str): The payload to send to each server
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[Any]: Results from all MCP servers
|
||||||
|
"""
|
||||||
|
if not params:
|
||||||
|
logger.warning("No MCP servers provided for batch operation")
|
||||||
|
return []
|
||||||
|
|
||||||
|
try:
|
||||||
|
return await asyncio.gather(*[_call_one_server(p, payload) for p in params])
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error in abatch_mcp_flow: {e}")
|
||||||
|
# Return partial results if any were successful
|
||||||
|
return [f"Error in batch operation: {str(e)}"]
|
||||||
|
|
||||||
|
|
||||||
|
def batch_mcp_flow(
|
||||||
|
params: List[MCPServerSseParams], payload: Dict[str, Any] | str
|
||||||
|
) -> List[Any]:
|
||||||
|
"""Sync wrapper for batch MCP operations.
|
||||||
|
|
||||||
|
This creates a new event loop if needed to run the async batch operation.
|
||||||
|
ONLY use this when not already in an async context.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
params (List[MCPServerSseParams]): List of MCP server configurations
|
||||||
|
payload (Dict[str, Any] | str): The payload to send to each server
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[Any]: Results from all MCP servers
|
||||||
|
"""
|
||||||
|
if not params:
|
||||||
|
logger.warning("No MCP servers provided for batch operation")
|
||||||
|
return []
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Check if we're already in an event loop
|
||||||
|
try:
|
||||||
|
loop = asyncio.get_event_loop()
|
||||||
|
except RuntimeError:
|
||||||
|
# No event loop exists, create one
|
||||||
|
loop = asyncio.new_event_loop()
|
||||||
|
asyncio.set_event_loop(loop)
|
||||||
|
|
||||||
|
if loop.is_running():
|
||||||
|
# We're already in an async context, can't use asyncio.run
|
||||||
|
# Use a future to bridge sync-async gap
|
||||||
|
future = asyncio.run_coroutine_threadsafe(
|
||||||
|
abatch_mcp_flow(params, payload), loop
|
||||||
|
)
|
||||||
|
return future.result(timeout=30) # Add timeout to prevent hanging
|
||||||
|
else:
|
||||||
|
# We're not in an async context, safe to use loop.run_until_complete
|
||||||
|
return loop.run_until_complete(abatch_mcp_flow(params, payload))
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error in batch_mcp_flow: {e}")
|
||||||
|
return [f"Error in batch operation: {str(e)}"]
|
Loading…
Reference in new issue