MCP HTTP Server

1.2.4 · active · verified Sun Apr 19

mcp-http-server is a high-performance HTTP server designed specifically for the Model Context Protocol (MCP) ecosystem, providing robust transport for both standard HTTP POST and Server-Sent Events (SSE). It offers flexible routing configurations, allows access to HTTP headers within the MCP server context, and supports custom Express-style middleware for extending server logic. The package currently maintains a stable version 1.2.4, suggesting a mature and consistent API for its core functionality. While originating from the broader UI-TARS-desktop project on GitHub, its specific release cadence appears independent of the more rapid beta iterations seen in related `@agent-tars` components. Key differentiators include its dual support for stateful and stateless modes for streamable HTTP, making it suitable for diverse real-time communication needs where MCP interactions are central.

Common errors

Warnings

Install

Imports

Quickstart

Initializes and starts a high-performance HTTP+SSE server for the Model Context Protocol (MCP) on port 3000, demonstrating basic setup with a minimal MCP server instance, access to request headers, and graceful shutdown.

import { startSseAndStreamableHttpMcpServer } from 'mcp-http-server';
import { Server } from '@modelcontextprotocol/sdk/server'; // Assumed correct import path for the SDK

// In a real application, createMcpServerInstance might involve more complex logic
// like setting up tools, agents, or database connections. For this example,
// we use a minimal MCP Server instance from the SDK.
const createMcpServerInstance = async (params: { headers: Record<string, string> }) => {
  console.log('Received request headers:', params.headers);
  // Return a new instance of an MCP Server (e.g., from @modelcontextprotocol/sdk/server)
  return new Server(
    { name: 'my-mcp-server', version: '1.1.0' },
    { capabilities: { tools: {} } } // Minimal capabilities for the example
  );
};

const main = async () => {
  try {
    const server = await startSseAndStreamableHttpMcpServer({
      port: 3000,
      host: '::', // Listen on all available network interfaces
      stateless: true, // Default to true, as per docs, enables streamable HTTP
      // Optionally add custom Express middlewares or route configurations:
      // middlewares: [ (req, res, next) => { console.log('Middleware hit:', req.method, req.url); next(); } ],
      // routes: { prefix: '/api/v1', mcp: '/my-mcp-endpoint' },
      createMcpServer: createMcpServerInstance,
    });
    console.log(`MCP HTTP Server running on http://localhost:3000. 
Access MCP endpoint at http://localhost:3000/mcp`);

    // Example: Keep the process alive. In a production app, handle graceful shutdown.
    process.on('SIGINT', async () => {
      console.log('Shutting down MCP HTTP server...');
      // Add server.close() logic if available in the returned server object
      process.exit(0);
    });
  } catch (error) {
    console.error('Failed to start MCP HTTP Server:', error);
    process.exit(1);
  }
};

main();

view raw JSON →