JSPM

  • Created
  • Published
  • Downloads 433
  • Score
    100M100P100Q88588F
  • License Apache-2.0

Jumpstart Model Context Protocol (MCP) development with this production-ready TypeScript template. Build robust MCP servers and clients with built-in utilities, authentication, and service integrations. Agent framework utilizing MCP Client included.

Package Exports

  • mcp-ts-template

Readme

mcp-ts-template

Build production-grade Model Context Protocol (MCP) servers with a powerful, type-safe, and extensible foundation.

TypeScript Model Context Protocol SDK MCP Spec Version Version Coverage License Status GitHub

This template provides a comprehensive foundation for building rich Model Context Protocol servers, adhering to the MCP 2025-06-18 specification and modern best practices. It includes a fully-featured server, production-ready utilities, and clear documentation to get you up and running quickly.

๐Ÿค” Why Use This Template?

Building a robust server for AI agents is more than just writing code. It requires a solid architecture, consistent error handling, and secure, type-safe practices from the ground up. This template solves these challenges by providing:

  • Accelerated Development: Skip the boilerplate and focus on your tool's core logic.
  • Production-Ready Foundation: Built-in logging, error handling, security, and testing.
  • Best Practices by Default: Enforces a clean, modular architecture that's easy to maintain and extend.
  • AI-Ready: Designed with LLM agents in mind, including detailed schemas and rich LLM developer-friendly resources (e.g. .clinerules).

Note on src/mcp-client & src/agent: The MCP client & Agent components have been enhanced and moved to the atlas-mcp-agent repository. This template now focuses exclusively on providing a best-in-class server implementation and framework.

โœจ Key Features

Feature Area Description Key Components / Location
๐Ÿ”Œ MCP Server A functional server with example tools and resources. Supports stdio and a Streamable HTTP transport built with Hono. src/mcp-server/, src/mcp-server/transports/
๐Ÿš€ Production Utilities Logging, Error Handling, ID Generation, Rate Limiting, Request Context tracking, Input Sanitization. src/utils/
๐Ÿ”’ Type Safety/Security Strong type checking via TypeScript & Zod validation. Built-in security utilities (sanitization, auth middleware for HTTP). Throughout, src/utils/security/, src/mcp-server/transports/auth/
โš™๏ธ Error Handling Consistent error categorization (BaseErrorCode), detailed logging, centralized handling (ErrorHandler). src/utils/internal/errorHandler.ts, src/types-global/
๐Ÿ“š Documentation Comprehensive README.md, structured JSDoc comments, API references. README.md, Codebase, tsdoc.json, docs/api-references/
๐Ÿ•ต๏ธ Interaction Logging Captures raw requests and responses for all external LLM provider interactions to a dedicated interactions.log file for full traceability. src/utils/internal/logger.ts
๐Ÿค– Agent Ready Includes a .clinerules developer cheatsheet tailored for LLM coding agents. .clinerules/
๐Ÿ› ๏ธ Utility Scripts Scripts for cleaning builds, setting executable permissions, generating directory trees, and fetching OpenAPI specs. scripts/
๐Ÿงฉ Services Reusable modules for LLM (OpenRouter) and data storage (DuckDB) integration, with examples. src/services/, src/storage/duckdbExample.ts
๐Ÿงช Integration Testing Integrated with Vitest for fast and reliable integration testing. Includes example tests for core logic and a coverage reporter. vitest.config.ts, tests/

Architecture Overview

This template employs a modular, transport-agnostic architecture to ensure a clean separation of concerns.

  • Core Server (src/mcp-server/server.ts): The central point where tools and resources are registered. It remains independent of how the server is accessed.
  • Transports (src/mcp-server/transports/): The transport layer connects the core server to the outside world.
    • StdioTransport: For direct process-to-process communication.
    • HttpTransport: A modern, streamable HTTP server built with Hono. It uses a middleware-based approach for handling CORS, rate limiting, authentication, and MCP request processing.
  • Transport Managers (src/mcp-server/transports/core/): These managers bridge the gap between the transport layer (like Hono) and the MCP SDK, handling session management (stateful vs. stateless) and request lifecycle.

This design allows you to add new tools and logic to the core server without worrying about the underlying transport details.

Quick Start

1. Installation

Clone the repository and install dependencies:

git clone https://github.com/cyanheads/mcp-ts-template.git
cd mcp-ts-template
npm install

2. Build the Project

npm run build
# Or use 'npm run rebuild' for a clean install

3. Running the Server

  • Via Stdio (Default):
    npm run start:server
  • Via Streamable HTTP:
    npm run start:server:http

4. Running Tests

This template uses Vitest for testing, with a strong emphasis on integration testing to ensure all components work together correctly.

  • Run all tests once:
    npm test
  • Run tests in watch mode:
    npm run test:watch
  • Run tests and generate a coverage report:
    npm run test:coverage

โš™๏ธ Configuration

Configure the server using these environment variables (or a .env file):

Variable Description Default
MCP_TRANSPORT_TYPE Server transport: stdio or http. stdio
MCP_SESSION_MODE Session mode for HTTP: stateless, stateful, or auto. auto
MCP_HTTP_PORT Port for the HTTP server. 3010
MCP_HTTP_HOST Host address for the HTTP server. 127.0.0.1
MCP_ALLOWED_ORIGINS Comma-separated allowed origins for CORS. (none)
MCP_AUTH_MODE Authentication mode for HTTP: jwt, oauth, or none. none
MCP_AUTH_SECRET_KEY Required for jwt mode. Secret key (min 32 chars) for signing/verifying auth tokens. (none - MUST be set in production)
OAUTH_ISSUER_URL Required for oauth mode. The issuer URL of your authorization server. (none)
OAUTH_AUDIENCE Required for oauth mode. The audience identifier for this MCP server. (none)
OPENROUTER_API_KEY API key for OpenRouter.ai service. (none)

๐Ÿ—๏ธ Project Structure

  • src/mcp-server/: Contains the core MCP server, tools, resources, and transport handlers.
  • src/config/: Handles loading and validation of environment variables.
  • src/services/: Reusable modules for integrating with external services (DuckDB, OpenRouter).
  • src/types-global/: Defines shared TypeScript interfaces and type definitions.
  • src/utils/: Core utilities (logging, error handling, security, etc.).
  • src/index.ts: The main entry point that initializes and starts the server.

Explore the full structure yourself:

See the current file tree in docs/tree.md or generate it dynamically:

npm run tree

๐Ÿงฉ Extending the System

The canonical pattern for adding new tools and resources is defined in the .clinerules file. It mandates a strict separation of concerns:

  1. logic.ts: Contains the pure business logic, Zod schemas, and type definitions. This file throws structured errors on failure.
  2. registration.ts: Acts as the "handler." It registers the tool with the server, wraps the logic call in a try...catch block, and formats the final success or error response.

This "Logic Throws, Handler Catches" pattern ensures that core logic remains pure and testable, while the registration layer handles all side effects and response formatting.

๐ŸŒ Explore More MCP Resources

Looking for more examples, guides, and pre-built MCP servers? Check out the companion repository:

โžก๏ธ cyanheads/model-context-protocol-resources

๐Ÿ“œ License

This project is licensed under the Apache License 2.0. See the LICENSE file for details.