mcp-server-service.ts


Overview

The mcp-server-service.ts file serves as a centralized service module for managing MCP (presumably "Managed Control Point" or similar) servers within an application. It abstracts and organizes API interactions related to MCP servers, encapsulating HTTP request details and endpoints for operations such as listing, creating, updating, deleting, importing, exporting MCP servers, as well as managing associated tools and testing server connectivity.

This file simplifies and standardizes how other parts of the application interact with MCP servers by providing a typed, consistent interface for making these API calls.


Detailed Explanation

Imports


methods Constant

This constant defines all available API methods related to MCP servers in a structured object, mapping each method to its corresponding URL and HTTP method.

const methods = {
  list: { url: listMcpServer, method: 'post' },
  get: { url: getMcpServer, method: 'get' },
  create: { url: createMcpServer, method: 'post' },
  update: { url: updateMcpServer, method: 'post' },
  delete: { url: deleteMcpServer, method: 'post' },
  import: { url: importMcpServer, method: 'post' },
  export: { url: exportMcpServer, method: 'post' },
  listTools: { url: listMcpServerTools, method: 'post' },
  testTool: { url: testMcpServerTool, method: 'post' },
  cacheTool: { url: cacheMcpServerTool, method: 'post' },
  test: { url: testMcpServer, method: 'post' },
} as const;

mcpServerService

This is the primary export of the file. It is a service object created by registerServer, which binds the methods defined above with the request utility to produce callable API functions.

const mcpServerService = registerServer<keyof typeof methods>(methods, request);
export default mcpServerService;

listMcpServers Function

This helper function provides an alternative way to list MCP servers with explicit parameters and request body.

export const listMcpServers = (params?: IPaginationRequestBody, body?: any) =>
  request.post(api.listMcpServer, { data: body || {}, params });
import { listMcpServers } from './mcp-server-service';

listMcpServers({ page: 1, size: 20 }, { status: 'active' })
  .then(response => console.log(response.data))
  .catch(error => console.error(error));

Important Implementation Details


Interaction with Other Parts of the System

This file acts as a bridge between the front-end UI or business logic layers and the back-end MCP server API.


Visual Diagram

classDiagram
    class MCPServerService {
        <<Service Object>>
        +list(params?: IPaginationRequestBody, body?: any)
        +get(id: string)
        +create(data: any)
        +update(data: any)
        +delete(id: string)
        +import(data: any)
        +export(params: any)
        +listTools(params: any)
        +testTool(data: any)
        +cacheTool(data: any)
        +test(data: any)
    }

    class Methods {
        +list
        +get
        +create
        +update
        +delete
        +import
        +export
        +listTools
        +testTool
        +cacheTool
        +test
    }

    class API {
        +listMcpServer
        +getMcpServer
        +createMcpServer
        +updateMcpServer
        +deleteMcpServer
        +importMcpServer
        +exportMcpServer
        +listMcpServerTools
        +testMcpServerTool
        +cacheMcpServerTool
        +testMcpServer
    }

    class Request {
        +post(url, options)
        +get(url, options)
    }

    API <.. Methods : provides URLs
    Methods --> MCPServerService : defines endpoints & HTTP methods
    Request <.. MCPServerService : performs HTTP requests

Summary

The mcp-server-service.ts file provides a clean, typed, and centralized service layer for managing MCP servers via API calls. It defines all relevant endpoints and HTTP methods in one place, uses utility functions for registering these methods and making requests, and exposes an easy-to-use interface for other modules to interact with MCP servers. This design promotes maintainability, consistency, and type safety across the application when dealing with MCP server operations.