MCP HubMCP Hub
QuantGeekDev

mcp-framework

by: QuantGeekDev

A framework for writing modelcontextprotocol (MCP) servers in Typescript

578created 08/12/2024
Visit
Typescript
framework

📌Overview

Purpose: The MCP Framework aims to provide an elegant way to build Model Context Protocol (MCP) servers using TypeScript.

Overview: MCP-Framework offers a robust structure for developing MCP servers, featuring automatic discovery of tools, resources, and prompts along with a command-line interface (CLI) that simplifies server setup. It utilizes TypeScript for type safety and is built on the official MCP SDK, enabling developers to create powerful applications with minimal effort.

Key Features:

  • Automatic Discovery: Automatically discovers and loads tools, resources, and prompts, streamlining the development process.

  • Multiple Transport Support: Offers flexibility with transport options such as stdio, Server-Sent Events (SSE), and HTTP Stream for varied communication needs.

  • TypeScript-first Development: Ensures full type safety, enhancing code quality and reducing runtime errors.

  • Base Classes for Abstractions: Provides easy-to-use base classes for defining tools, resources, and prompts efficiently.

  • Out-of-the-box Authentication: Includes built-in authentication mechanisms, particularly for SSE endpoints, ensuring secure interactions.


MCP Framework

MCP-Framework is a framework for building Model Context Protocol (MCP) servers in TypeScript.

Features

  • Automatic discovery and loading of tools, resources, and prompts
  • Multiple transport support (stdio, SSE, HTTP Stream)
  • TypeScript-first development with full type safety
  • Built on the official MCP SDK
  • Easy-to-use base classes for tools, prompts, and resources
  • Out of the box authentication for SSE endpoints

Quick Start

Creating a Repository with MCP Framework

# Install the framework globally
npm install -g mcp-framework

# Create a new MCP server project
mcp create my-mcp-server

# Navigate to your project
cd my-mcp-server

CLI Usage

Project Creation

mcp create <your project name>

Adding a Tool

mcp add tool price-fetcher

Adding a Prompt

mcp add prompt price-analysis

Adding a Resource

mcp add resource market-data

Development Workflow

  1. Create your project:

    mcp create my-mcp-server
    cd my-mcp-server
    
  2. Add tools as needed:

    mcp add tool data-fetcher
    mcp add tool data-processor
    mcp add tool report-generator
    
  3. Build:

    npm run build
    

Environment Variables

The framework supports the following environment variables for configuration:

VariableDescriptionDefault
MCP_ENABLE_FILE_LOGGINGEnable logging to files (true/false)false
MCP_LOG_DIRECTORYDirectory where log files will be storedlogs
MCP_DEBUG_CONSOLEDisplay debug level messages in console (true/false)false

Example usage:

# Enable file logging
MCP_ENABLE_FILE_LOGGING=true node dist/index.js

# Specify a custom log directory
MCP_LOG_DIRECTORY=my-logs

Authentication

MCP Framework provides optional authentication for SSE endpoints. You can choose between JWT and API Key authentication, or implement your own custom provider.

JWT Authentication

import { MCPServer, JWTAuthProvider } from "mcp-framework";

const server = new MCPServer({
  transport: {
    type: "sse",
    options: {
      auth: {
        provider: new JWTAuthProvider({
          secret: process.env.JWT_SECRET,
          algorithms: ["HS256"],
          headerName: "Authorization"
        }),
        endpoints: {
          sse: true,
          messages: true
        }
      }
    }
  }
});

API Key Authentication

import { MCPServer, APIKeyAuthProvider } from "mcp-framework";

const server = new MCPServer({
  transport: {
    type: "sse",
    options: {
      auth: {
        provider: new APIKeyAuthProvider({
          keys: [process.env.API_KEY],
          headerName: "X-API-Key"
        })
      }
    }
  }
});

Custom Authentication

Implement the AuthProvider interface for custom authentication logic:

import { AuthProvider, AuthResult } from "mcp-framework";
import { IncomingMessage } from "node:http";

class CustomAuthProvider implements AuthProvider {
  async authenticate(req: IncomingMessage): Promise<boolean | AuthResult> {
    // Custom authentication logic
    return true;
  }

  getAuthError() {
    return {
      status: 401,
      message: "Authentication failed"
    };
  }
}

License

MIT