mcp_server_app.py


Overview

mcp_server_app.py is a Flask-based RESTful API module dedicated to managing MCP (Multi-Cloud Platform) servers within the InfiniFlow application. The file provides endpoints for CRUD (Create, Read, Update, Delete) operations on MCP server entities, importing/exporting MCP servers, listing and caching tools associated with MCP servers, and testing connectivity and functionality of MCP servers and their tools.

The module acts as a controller layer interacting with the database services (MCPServerService, TenantService), utility functions for request validation and response formatting, and the MCPToolCallSession for communicating with MCP tools.


Detailed Description of Components

Flask Blueprint and Decorators


Endpoint Functions


list_mcp() -> Response

Route: /list (POST)
Purpose: List MCP servers belonging to the current logged-in user with optional filtering, pagination, and sorting.
Parameters (via URL query):

Request JSON Body:

Returns:

Example Usage:

POST /list?keywords=test&page=1&page_size=10&orderby=name&desc=false
Content-Type: application/json

{
    "mcp_ids": ["id1", "id2"]
}

detail() -> Response

Route: /detail (GET)
Purpose: Fetch MCP server details by MCP server ID.
Parameters (via URL query):

Returns:


create() -> Response

Route: /create (POST)
Purpose: Create a new MCP server for the current user.
Required JSON fields: "name", "url", "server_type" (validated by @validate_request)

Input JSON fields:

Process:

Returns:


update() -> Response

Route: /update (POST)
Purpose: Update an existing MCP server owned by the current user.
Required JSON fields: "mcp_id" (validated)

Input JSON fields (optional):

Process:


rm() -> Response

Route: /rm (POST)
Purpose: Delete MCP servers by IDs for the current user.
Required JSON fields: "mcp_ids" (list of MCP server IDs)

Returns:


import_multiple() -> Response

Route: /import (POST)
Purpose: Bulk import MCP servers from a provided dictionary of server configurations.
Required JSON fields: "mcpServers" (dictionary keyed by server name)

Behavior:

Returns:


export_multiple() -> Response

Route: /export (POST)
Purpose: Export MCP server configurations by IDs owned by the current user.
Required JSON fields: "mcp_ids" (list)

Returns:


list_tools() -> Response

Route: /list_tools (POST)
Purpose: Retrieve the list of tools available from specified MCP servers.
Required JSON fields: "mcp_ids" (list)

Behavior:

Returns:


test_tool() -> Response

Route: /test_tool (POST)
Purpose: Test execution of a specific tool on a specified MCP server.
Required JSON fields: "mcp_id", "tool_name", "arguments"

Behavior:


cache_tool() -> Response

Route: /cache_tools (POST)
Purpose: Cache tool metadata for a MCP server to speed up future tool listings.
Required JSON fields: "mcp_id", "tools" (list of tool dicts)

Behavior:


test_mcp() -> Response

Route: /test_mcp (POST)
Purpose: Test MCP server connectivity and retrieve tools without saving the MCP server.
Required JSON fields: "url", "server_type"

Behavior:


Important Implementation Details and Algorithms


Interaction with Other System Components


Visual Diagram

classDiagram
    class MCPServerService {
        +get_servers(user_id, mcp_ids, offset, limit, orderby, desc, keywords)
        +get_or_none(id, tenant_id)
        +get_by_name_and_tenant(name, tenant_id)
        +get_by_id(id)
        +insert(**kwargs)
        +filter_update(filters, data)
        +delete_by_ids(mcp_ids)
    }

    class TenantService {
        +get_by_id(id)
    }

    class MCPServer {
        +id: str
        +name: str
        +url: str
        +server_type: str
        +variables: dict
        +headers: dict
        +to_dict()
    }

    class MCPToolCallSession {
        +__init__(mcp_server, variables)
        +get_tools(timeout)
        +tool_call(tool_name, arguments, timeout)
    }

    class FlaskEndpoints {
        +list_mcp()
        +detail()
        +create()
        +update()
        +rm()
        +import_multiple()
        +export_multiple()
        +list_tools()
        +test_tool()
        +cache_tool()
        +test_mcp()
    }

    FlaskEndpoints --> MCPServerService : uses
    FlaskEndpoints --> TenantService : uses
    FlaskEndpoints --> MCPServer : uses
    FlaskEndpoints --> MCPToolCallSession : uses

Summary

mcp_server_app.py is a critical backend module for managing MCP servers in InfiniFlow. It provides robust REST API endpoints to create, read, update, delete, import/export MCP servers, manage their tools, and test connections. The module integrates tightly with database services, authentication, and a specialized tool calling session to interact with MCP server toolsets, while ensuring tenant isolation and input validation.

This module serves as the bridge between frontend MCP server management UI and the underlying MCP server infrastructure and metadata.