@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 supportAuto-Discovery
Zero-config service discovery from
./mcp directorySchema Generation
Declarative JSON Schema with
@SchemaConstraint decoratorsHTTP Transport
Production-ready HTTP server with session management
Installation
Quick Start
Zero-Config (Recommended)
The simplest way to create an MCP server with auto-discovery:Define a Service
Decorators
@Tool
Marks a method as a callable MCP tool.| Option | Type | Description |
|---|---|---|
description | string | Tool description for the AI |
inputClass | Class | Class defining input schema |
@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.description,default- DocumentationminLength,maxLength- String lengthminimum,maximum- Number rangeenum- Allowed valuesformat- 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):MCPServer
Main server class for registering services.Auto-Discovery
Services are automatically discovered from the./mcp directory:
- Recursively scans for
index.tsorindex.jsfiles - Dynamically imports each file
- Looks for exported classes
- Instantiates with no-args constructors
- Registers all decorated methods
Shared Dependencies
For services needing shared configuration (auth, database, etc.), create aconfig.ts:
HTTP Endpoints
| Endpoint | Method | Description |
|---|---|---|
/mcp | POST | MCP protocol endpoint (JSON-RPC 2.0) |
/health | GET | Health check |
/ | GET | Welcome message |
Error Handling
Errors are automatically caught and returned in MCP format:Environment Variables
TypeScript Support
Key Points:
- Input schema is defined via
inputClassin the decorator - Output type is inferred from the return type
- For tools with no input, omit
inputClass - Use
@SchemaConstraintfor validation and documentation
Related Packages
- @leanmcp/cli - CLI tool for project creation
- @leanmcp/auth - Authentication decorators
- @leanmcp/ui - MCP App UI components
- @leanmcp/elicitation - Structured user input