Skip to main content

@leanmcp/core

Core library for building Model Context Protocol (MCP) servers with TypeScript decorators and declarative schema definition.

Features

Type-Safe Decorators

@Tool, @Prompt, @Resource with full TypeScript support

Auto-Discovery

Zero-config service discovery from ./mcp directory

Schema Generation

Declarative JSON Schema with @SchemaConstraint decorators

HTTP Transport

Production-ready HTTP server with session management

Installation

For HTTP server support:

Quick Start

The simplest way to create an MCP server with auto-discovery:
Directory Structure:

Define a Service


Decorators

@Tool

Marks a method as a callable MCP tool.
Options:

@Prompt

Marks a method as a reusable prompt template.

@Resource

Marks a method as an MCP resource (data source).

@SchemaConstraint

Add validation constraints to class properties.
Common constraints:
  • description, default - Documentation
  • minLength, maxLength - String length
  • minimum, maximum - Number range
  • enum - Allowed values
  • format - String format (email, uri, date, etc.)
  • pattern - Regex pattern

@Optional

Marks a property as optional in the schema.

API Reference

createHTTPServer

Create and start an HTTP server with auto-discovery. Simplified API (Recommended):
Factory Pattern (Advanced):

MCPServer

Main server class for registering services.

Auto-Discovery

Services are automatically discovered from the ./mcp directory:
  1. Recursively scans for index.ts or index.js files
  2. Dynamically imports each file
  3. Looks for exported classes
  4. Instantiates with no-args constructors
  5. Registers all decorated methods

Shared Dependencies

For services needing shared configuration (auth, database, etc.), create a config.ts:
Then import in your services:

HTTP Endpoints

Error Handling

Errors are automatically caught and returned in MCP format:
Returns:

Environment Variables

TypeScript Support

Key Points:
  • Input schema is defined via inputClass in the decorator
  • Output type is inferred from the return type
  • For tools with no input, omit inputClass
  • Use @SchemaConstraint for validation and documentation