agentscope.service.mcp_manager

This module manages MCP (ModelContextProtocal) sessions and tool execution within an asynchronous context. It includes functionality to create, manage, and close sessions, as well as execute various tools provided by an MCP server.

class MCPSessionHandler(name: str, config: dict[str, Any], sync: bool = True)[source]

Bases: object

Handles MCP session connections and tool execution.

async cleanup() None[source]

Clean up server stream resources.

async execute_tool(tool_name: str, **kwargs: Any) ServiceResponse[source]

Execute a tool and return ServiceResponse

async initialize() None[source]

Initialize stdio_transport

async list_tools() list[Any][source]

List available tools from the server.

sync_exec(func: Callable, *args: Any, **kwargs: Any) Any[source]

Execute a function synchronously.

Parameters:
  • func (Callable) – The asynchronous function to execute.

  • *args (Any) – Positional arguments to pass to the function.

  • **kwargs (Any) – Keyword arguments to pass to the function.

Returns:

The result of the function execution.

Return type:

Any