Skip to content

Conversation

@luv-bansal
Copy link
Contributor

@luv-bansal luv-bansal commented Dec 9, 2025

Summary

This PR introduces a major architectural improvement to the MCPModelClass by implementing persistent session management and adding support for bridging stdio MCP servers to Python FastMCP.

Problem

The previous MCPModelClass implementation suffered from significant performance overhead:

  • Created a new asyncio event loop for every request via asyncio.run()
  • Opened and closed FastMCP client sessions on each request using async with self.client
  • No connection reuse.
  • Occasional "Session terminated" errors due to rapid session cycling

For opensourced Sdtio MCP servers, there was no built-in support in clarifai, the only way to host MCP server with Clarifai is by implementing own custom server with implementing all tools. 

Solution

1. Refactored MCPModelClass with Persistent Sessions

  • Background thread with long-lived event loop: A dedicated daemon thread now owns a single asyncio event loop that runs for the entire model lifetime
  • Persistent FastMCP client session: The client session is opened once during load_model() and reused for all subsequent requests
  • Efficient cross-thread dispatch: Requests from the main thread are dispatched to the background loop using asyncio.run_coroutine_threadsafe()
  • Proper lifecycle management: Added shutdown() method for clean resource cleanup

2. New StdioMCPModelClass for stdio MCP Servers

  • Automatic tool discovery: Discovers all tools from a stdio MCP server at startup and registers them in Python FastMCP
  • Single long-lived Node.js process: The stdio process (e.g., npx -y @modelcontextprotocol/server-github) starts once and stays running
  • Persistent MCP session: Maintains a single ClientSession to the stdio server with double-checked locking for thread safety
  • Dynamic function generation: Creates properly-typed Python functions for each stdio tool with correct signatures and annotations
  • Configuration via YAML: Simple setup through config.yaml with support for environment variables and secrets

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a new bridge class for integrating Node.js MCP (Model Context Protocol) servers with Python FastMCP servers within the Clarifai platform. The implementation automatically discovers and registers tools from Node.js MCP servers, making them available to MCP clients through a Python interface.

Key Changes:

  • Adds NodeJSMCPModelClass that extends MCPModelClass to bridge Node.js and Python MCP servers
  • Implements NodeMCPClient for communicating with Node.js MCP servers via stdio
  • Provides automatic tool discovery and registration with dynamic function generation
Comments suppressed due to low confidence (2)

clarifai/runners/models/nodejs_mcp_class.py:59

  • 'except' clause does nothing but pass and there is no explanatory comment.
            except Exception:

clarifai/runners/models/nodejs_mcp_class.py:63

  • 'except' clause does nothing but pass and there is no explanatory comment.
            except Exception:

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 25 comments.

Comments suppressed due to low confidence (1)

clarifai/runners/models/mcp_class.py:258

  • 'except' clause does nothing but pass and there is no explanatory comment.
        except Exception:

@luv-bansal luv-bansal changed the title Nodejsmcpclass Optimised MCPModelClass and supports for Sdtio MCP servers Dec 11, 2025
Copy link
Contributor

Copilot AI commented Dec 11, 2025

@luv-bansal I've opened a new pull request, #873, to work on those changes. Once the pull request is ready, I'll request review from you.

Copy link
Contributor

Copilot AI commented Dec 11, 2025

@luv-bansal I've opened a new pull request, #874, to work on those changes. Once the pull request is ready, I'll request review from you.

@github-actions
Copy link

Code Coverage

Package Line Rate Health
clarifai 45%
clarifai.cli 58%
clarifai.cli.templates 46%
clarifai.client 67%
clarifai.client.auth 67%
clarifai.constants 100%
clarifai.datasets 100%
clarifai.datasets.export 80%
clarifai.datasets.upload 75%
clarifai.datasets.upload.loaders 37%
clarifai.models 100%
clarifai.modules 0%
clarifai.rag 0%
clarifai.runners 54%
clarifai.runners.models 62%
clarifai.runners.pipeline_steps 41%
clarifai.runners.pipelines 77%
clarifai.runners.utils 62%
clarifai.runners.utils.data_types 72%
clarifai.schema 100%
clarifai.urls 60%
clarifai.utils 60%
clarifai.utils.evaluation 67%
clarifai.workflows 95%
Summary 62% (8878 / 14286)

Minimum allowed line rate is 50%

@luv-bansal luv-bansal changed the title Optimised MCPModelClass and supports for Sdtio MCP servers [PR-1092][PR-1093] Optimised MCPModelClass and supports for Sdtio MCP servers Dec 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants