MCP Protocol Plugin
Relevant source files
The following files were used as context for generating this wiki page:
The utcp-mcp plugin provides interoperability between the Universal Tool Calling Protocol (UTCP) and the Model Context Protocol (MCP). It acts as a bridge, allowing a UTCP client to discover and call tools exposed by existing MCP servers without modification. This enables gradual migration from MCP to UTCP and allows projects to leverage tools from both ecosystems simultaneously.
The plugin supports connecting to MCP servers via two transport mechanisms: stdio for local, process-based servers and http for remote servers. It features automatic tool discovery, efficient session management, and support for OAuth2 authentication.
Sources: plugins/communication_protocols/mcp/README.md, plugins/communication_protocols/mcp/src/utcp_mcp/mcp_communication_protocol.py:22-29
Architecture and Core Components
The plugin's functionality is centered around the McpCommunicationProtocol class, which implements the UTCP CommunicationProtocol interface. It uses the mcp-use library's MCPClient to handle the underlying communication and session management with MCP servers.
Sources: plugins/communication_protocols/mcp/src/utcp_mcp/mcp_communication_protocol.py:31, plugins/communication_protocols/mcp/pyproject.toml:12
Key Classes
| Class | Description | File |
|---|---|---|
McpCommunicationProtocol | The main implementation of the communication protocol. Manages sessions and translates UTCP calls to MCP calls. | mcp_communication_protocol.py |
McpCallTemplate | A Pydantic model for configuring connections to MCP servers. It extends the base CallTemplate. | mcp_call_template.py |
McpConfig | A data model nested within McpCallTemplate that holds the mcpServers configuration dictionary. | mcp_call_template.py |
Class Diagram
The following diagram illustrates the relationship between the main classes in the MCP plugin.
Configuration
The MCP plugin is configured through the manual_call_templates section in the UtcpClient configuration. The call_template_type must be set to mcp.
Sources: plugins/communication_protocols/mcp/README.md, core/README.md:104-114
McpCallTemplate Fields
| Field | Type | Required | Description |
|---|---|---|---|
name | string | Yes | A unique name for this manual configuration. |
call_template_type | string | Yes | Must be "mcp". |
config | object | Yes | Contains the mcpServers dictionary. |
auth | object | No | Authentication configuration, e.g., for OAuth2. |
register_resources_as_tools | boolean | No | If true, MCP resources are discovered and registered as callable tools. |
Sources: plugins/communication_protocols/mcp/src/utcp_mcp/mcp_communication_protocol.py:239, plugins/communication_protocols/mcp/README.md
Configuration Examples
Stdio Transport (Local Process)
This configuration connects to an MCP server running as a local Python process.
{
"name": "local_mcp",
"call_template_type": "mcp",
"config": {
"mcpServers": {
"filesystem": {
"command": "python",
"args": ["-m", "mcp_filesystem_server"],
"env": {"LOG_LEVEL": "INFO"}
}
}
}
}
Sources: plugins/communication_protocols/mcp/README.md
HTTP Transport (Remote Server)
This example connects to a remote MCP server over HTTP. The test suite provides a working example of this.
{
"name": "remote_mcp",
"call_template_type": "mcp",
"config": {
"mcpServers": {
"api_server": {
"transport": "http",
"url": "https://mcp.example.com"
}
}
}
}
Sources: plugins/communication_protocols/mcp/README.md, plugins/communication_protocols/mcp/tests/test_mcp_http_transport.py:53-61
OAuth2 Authentication
This configuration adds OAuth2 client credentials authentication for an HTTP transport.
{
"name": "secure_mcp",
"call_template_type": "mcp",
"config": {
"mcpServers": {
"secure_server": {
"transport": "http",
"url": "https://mcp.example.com"
}
}
},
"auth": {
"auth_type": "oauth2",
"token_url": "https://auth.example.com/token",
"client_id": "${CLIENT_ID}",
"client_secret": "${CLIENT_SECRET}",
"scope": "read:tools"
}
}
Sources: plugins/communication_protocols/mcp/README.md, plugins/communication_protocols/mcp/src/utcp_mcp/mcp_communication_protocol.py:481-483
Tool Discovery and Registration
When a McpCallTemplate is provided to the UtcpClient, the McpCommunicationProtocol.register_manual method is invoked. This method iterates through each server defined in mcpServers, lists its available tools, and registers them with the UTCP client.
This diagram shows the tool discovery and registration flow.
Key aspects of this process include:
- Tool Namespacing: To avoid conflicts, each tool's name is automatically prefixed with its server name, following the pattern
<server_name>.<tool_name>. - Resource as Tools: If
register_resources_as_toolsis true, the plugin also lists MCP resources and creates corresponding tools (e.g.,resource_<name>) that read the resource content when called. - Error Handling: If tool discovery fails for one server, it is logged as an error, but the process continues for other configured servers.
Tool Execution
Calling an MCP tool is handled by the call_tool method. The process involves parsing the tool name to identify the target server and the actual tool name, retrieving or creating a session, and then executing the call.
This flowchart shows the logic for executing a tool call.
The _process_tool_result method intelligently parses the output from the MCP server, handling structured JSON, plain text, numbers, and lists to return a clean Python object.
Sources: plugins/communication_protocols/mcp/src/utcp_mcp/mcp_communication_protocol.py:402-458
Streaming is not natively supported by MCP. The call_tool_streaming method simply wraps a standard call_tool call and yields the single result.
Sources: plugins/communication_protocols/mcp/src/utcp_mcp/mcp_communication_protocol.py:397-400
Session Management and Performance
To optimize performance, the plugin reuses MCPClient sessions. A session is created for a server on the first request and is reused for subsequent calls to any tool on that same server.
_ensure_mcp_client(): Initializes theMCPClientfrom the configuration._get_or_create_session(): Retrieves an active session or creates a new one if none exists._cleanup_session()/_cleanup_all_sessions(): Methods to close sessions, either individually or all at once.
If a session-level error (e.g., connection timeout, broken pipe) occurs, the plugin automatically cleans up the faulty session and retries the operation once with a fresh session.
Sources: plugins/communication_protocols/mcp/src/utcp_mcp/mcp_communication_protocol.py:48-94, 120-138
Migration from MCP to UTCP
The utcp-mcp plugin is designed to facilitate a smooth, phased migration from an MCP-based architecture to a native UTCP one.
- Phase 1: MCP Integration: Use the
utcp-mcpplugin to wrap all existing MCP servers. The application now uses the UTCP client interface, but all tool calls are proxied to MCP servers. - Phase 2: Mixed Environment: As new tools are developed or old ones are migrated, they can be implemented as native UTCP tools (e.g., using
utcp-http). The UTCP client can seamlessly handle a mix ofmcpandhttpcall templates, calling both legacy MCP tools and new UTCP tools. - Phase 3: Full UTCP: Once all tools have been migrated to native UTCP protocols, the
utcp-mcpplugin and its configurations can be removed.