Skip to content

tsok-org/model-context-protocol

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

56 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Model Context Protocol (MCP) β€” Enterprise Infrastructure

Production-ready, horizontally scalable TypeScript implementation for building MCP servers and clients

License: AGPL v3 TypeScript MCP Spec Nx


🎯 Mission

The Model Context Protocol (MCP) is revolutionizing how AI agents interact with tools, data, and services. As adoption accelerates, the need for enterprise-grade infrastructure becomes critical.

This project provides a complete, production-ready MCP implementation designed from the ground up for:

  • Horizontal scalability β€” Run multiple server instances behind load balancers
  • Session persistence β€” Maintain client state across restarts and scaling events
  • Full observability β€” Correlate logs, metrics, and traces across distributed systems
  • Protocol compliance β€” Wire-compatible with the official MCP SDK
  • Extensibility β€” Add custom features without forking core protocol logic

Our goal: Make it easy to build scalable, reliable, and observable MCP serversβ€”from startup MVPs to enterprise deployments.


πŸ—οΈ Architecture

We took a protocol-first approach: instead of building high-level abstractions that hide the protocol, we built a modular, layered architecture that gives you full control while reducing boilerplate.

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        Your Application                              β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  Server/Client Classes  β”‚  Feature System  β”‚  Custom Extensions      β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                      Protocol Layer                                  β”‚
β”‚  (Connection management, request/response correlation, lifecycle)    β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  Schema Validation (Standard Schema)  β”‚  Session Management          β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                      Transport Layer                                 β”‚
β”‚  (Pluggable: HTTP, stdio, distributed brokers, WebRTC, etc.)        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Distributed Deployment Architecture

                                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                    β”‚         Load Balancer           β”‚
                                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                                  β”‚
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚                             β”‚                             β”‚
           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”
           β”‚   HTTP Node 1   β”‚           β”‚   HTTP Node 2   β”‚           β”‚   HTTP Node 3   β”‚
           β”‚   (Transport)   β”‚           β”‚   (Transport)   β”‚           β”‚   (Transport)   β”‚
           β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜           β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜           β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                    β”‚                             β”‚                             β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                                  β”‚
                                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                    β”‚         EventBroker             β”‚
                                    β”‚   (NATS / Kafka / Redis / ...)  β”‚
                                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                                  β”‚
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚                             β”‚                             β”‚
           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”
           β”‚    Worker 1     β”‚           β”‚    Worker 2     β”‚           β”‚    Worker 3     β”‚
           β”‚  (MCP Handler)  β”‚           β”‚  (MCP Handler)  β”‚           β”‚  (MCP Handler)  β”‚
           β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜           β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜           β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“¦ Packages

This monorepo contains a suite of packages for building MCP infrastructure:

Core Packages

Package npm Description
model-context-protocol-specification npm Canonical TypeScript types and Zod v4 schemas for all MCP message types. Foundation for type-safe MCP development.
model-context-protocol-sdk npm Core SDK with Protocol, Server, and Client implementations. Feature-based architecture for tools, resources, prompts, and completions.
model-context-protocol-framework npm Higher-level abstractions and patterns for common MCP use cases. (Coming soon)

Transport Packages

Package npm Description
model-context-protocol-distributed-streamable-http-server-transport npm Enterprise HTTP transport with EventBroker abstraction for horizontal scaling. Supports NATS, Kafka, Redis, or custom brokers.
model-context-protocol-webrtc-transport npm WebRTC transport for real-time, peer-to-peer MCP connections. (In development)

Observability

Package npm Description
model-context-protocol-opentelemetry-instrumentation npm OpenTelemetry instrumentation for automatic tracing, metrics, and log correlation. (In development)

πŸ”‘ Key Design Decisions

1. Sessions as First-Class Citizens

In distributed systems, sessions are everything. They're your routing key, persistence boundary, and observability anchor.

// Every handler receives full session context
tools.registerTool(myTool, async (args, context, info) => {
  const session = context.session;
  
  // Access session state
  const userPrefs = session.getValue<UserPrefs>('preferences');
  
  // Session metadata for observability
  console.log(`[${session.id}] Processing tool call`);
  
  return await processWithSession(args, session);
});

2. Feature-Based Architecture

Instead of monolithic handlers, we use a composable feature system:

const server = new Server({
  serverInfo: { name: "my-server", version: "1.0.0" },
  capabilities: {
    tools: { listChanged: true },
    resources: { subscribe: true },
    prompts: {}
  }
});

// Features are self-contained and reusable
server.addFeature(new ToolsFeature(myTools));
server.addFeature(new ResourcesFeature(myResources));
server.addFeature(new PromptsFeature(myPrompts));
server.addFeature(new MyCustomFeature());

3. Transport-Agnostic Design

The protocol layer knows nothing about HTTP, WebSockets, or message brokers:

// Same server logic, different transports
const server = new Server({ /* ... */ });

// Local development with stdio
await server.connect(stdioTransport);

// Production with distributed HTTP
await server.connect(distributedHttpTransport);

// Real-time with WebRTC
await server.connect(webRTCTransport);

4. Explicit Validation with Standard Schema

We don't hide validationβ€”we make it explicit and pluggable:

import { StandardSchemaValidator, defaultSchemaResolver } from "model-context-protocol-sdk/protocol";

const server = new Server({
  schemaValidator: new StandardSchemaValidator(),
  schemaResolver: defaultSchemaResolver,
  enforceSchemaValidation: true
});

Compatible with Zod v4, Valibot, ArkType, or any Standard Schema-compatible validator.

5. Instrumentation-Ready Architecture

Every layer is designed for observability with lifecycle hooks:

  • onBeforeSendRequest / onAfterSendRequest
  • onBeforeSendNotification / onAfterSendNotification
  • onBeforeReceive / onAfterReceive

πŸ†š Comparison with Official SDK

Aspect Official SDK This SDK
Goal Quick start, batteries included Enterprise scale, full control
Transport Bundled transports Transport interface + plugins
Sessions Transport-managed First-class protocol concept
Validation Optional, built-in Explicit, pluggable (Standard Schema)
Handlers Direct method handlers Feature-based composition
Distributed Single-node focus Distributed-first design
Observability Basic callbacks Structured hooks + correlation

When to Use Which

Use the Official SDK when:

  • Building a quick prototype or demo
  • Single-node deployment is sufficient
  • You want maximum convenience with minimal configuration

Use this SDK when:

  • Building production infrastructure
  • Planning horizontal scaling
  • Need fine-grained control over protocol behavior
  • Integrating with existing observability stack
  • Building custom transports or extensions

πŸš€ Quick Start

Installation

npm install model-context-protocol-sdk
# For runtime validation (recommended)
npm install zod @standard-schema/spec

Server Example

import { Server, ToolsFeature } from "model-context-protocol-sdk/server";

const server = new Server({
  serverInfo: { name: "my-server", version: "1.0.0" },
  capabilities: { tools: { listChanged: true } },
  instructions: "This server provides utility tools."
});

const tools = new ToolsFeature();

tools.registerTool(
  {
    name: "greet",
    description: "Generate a greeting",
    inputSchema: {
      type: "object",
      properties: { name: { type: "string" } },
      required: ["name"]
    }
  },
  async (args) => ({
    content: [{ type: "text", text: `Hello, ${(args as any).name}!` }]
  })
);

server.addFeature(tools);
await server.connect(transport);

Client Example

import { Client } from "model-context-protocol-sdk/client";

const client = new Client();
await client.connect(transport);

const result = await client.request({
  method: "tools/call",
  params: { name: "greet", arguments: { name: "World" } }
}, { route: { sessionId } });

πŸ“ Repository Structure

model-context-protocol/
β”œβ”€β”€ packages/
β”‚   β”œβ”€β”€ specification/          # Types & Zod schemas
β”‚   β”œβ”€β”€ sdk/                    # Core Protocol, Server, Client
β”‚   β”œβ”€β”€ framework/              # High-level abstractions
β”‚   β”œβ”€β”€ transports/
β”‚   β”‚   β”œβ”€β”€ server/
β”‚   β”‚   β”‚   └── distributed-streamable-http/
β”‚   β”‚   └── webrtc-transport/
β”‚   └── opentelemetry/
β”‚       └── instrumentation/
β”œβ”€β”€ examples/
β”‚   └── server/                 # Complete example server
β”œβ”€β”€ e2e/
β”‚   └── server/                 # E2E tests with official & our client
β”œβ”€β”€ nx.json                     # Nx workspace config
└── package.json

πŸ§ͺ Testing & Development

Prerequisites

  • Node.js 20+
  • pnpm 9+

Setup

pnpm install

Build All Packages

pnpm nx run-many -t build

Run Tests

# Unit tests
pnpm nx run-many -t test

# E2E tests (start server first)
pnpm nx serve server  # Terminal 1
pnpm nx test e2e-server  # Terminal 2

Run Example Server

pnpm nx serve server
# Server runs at http://localhost:3333/mcp

πŸ”¬ Our Findings

Through building this implementation, we discovered several insights about MCP in production:

Session Management is Critical

The MCP spec leaves session management largely to implementers. In distributed systems, this becomes the central challenge. We found that treating sessions as first-class routing keys (rather than transport concerns) dramatically simplifies horizontal scaling.

Validation at Protocol Boundaries

Protocol boundaries are trust boundaries. While the official SDK offers optional validation, production systems benefit from explicit, always-on validation. Our Standard Schema integration makes this pluggable while maintaining performance.

Feature Composition > Monolithic Handlers

Large MCP servers with many tools/resources become unwieldy with direct handler registration. Our feature-based architecture enables:

  • Testing features in isolation
  • Reusing features across servers
  • Third-party feature packages

Transport Abstraction Enables Innovation

By strictly separating transport from protocol, we enabled transports the official SDK doesn't support:

  • Distributed HTTP: Multiple nodes sharing a message broker
  • WebRTC: Browser-to-server without HTTP
  • Custom brokers: NATS, Kafka, Redis Streams, etc.

πŸ—ΊοΈ Roadmap

  • Core SDK (Protocol, Server, Client)
  • Specification package with Zod v4 schemas
  • Distributed Streamable HTTP transport
  • E2E test suite with official SDK compatibility
  • OpenTelemetry instrumentation
  • WebRTC transport
  • Framework package with common patterns
  • Stdio transport
  • Authentication middleware examples
  • Kubernetes deployment guides

🀝 Contributing

We're building this in the open because we believe MCP infrastructure should be a community effort.

  • Issues: Bug reports, feature requests, questions
  • PRs: Bug fixes, documentation, new features
  • Discussions: Architecture decisions, use cases, best practices

πŸ“š Documentation


πŸ“„ License

This project is fully open-source under the GNU Affero General Public License v3.0 (AGPLv3). See LICENSE for the complete terms.

If you're interested in an enterprise license with different terms, please contact: operator@tsok.org

About

Model Context Protocol SDK and Framework designed for Enterprise production deployments

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •