@leanmcp/core
Core library for building Model Context Protocol (MCP) servers with TypeScript decorators and declarative schema definition.Features
- Type-safe decorators -
@Tool,@Prompt,@Resourcewith full TypeScript support - Schema generation - Define JSON Schema declaratively using
@SchemaConstraintdecorators on class properties - Streamable HTTP transport - Production-ready HTTP server with session management
- Input validation - Built-in AJV validation for all inputs
- Clean API - Function names automatically become tool/prompt/resource names
- MCP compliant - Built on official
@modelcontextprotocol/sdk
Installation
Peer Dependencies
For HTTP server support:Quick Start
1. Define Your Service with Class-Based Schema
2. Create and Start Server
Decorators
@Tool
Marks a method as an MCP tool (callable function). UseinputClass to specify the input schema class.
@Prompt
Marks a method as an MCP prompt template. Input schema is automatically inferred from parameter type.@Resource
Marks a method as an MCP resource (data source).@SchemaConstraint
Add validation constraints to class properties for automatic schema generation.@Optional
Marks a property as optional in the schema.API Reference
MCPServer
Main server class for registering services.createHTTPServer
Create and start an HTTP server with streamable transport.Schema Generation
Generate JSON Schema from TypeScript classes:HTTP Endpoints
When usingcreateHTTPServer, the following endpoints are available:
POST /mcp- MCP protocol endpoint (accepts JSON-RPC 2.0 messages)GET /health- Health check endpointGET /- Welcome message
Environment Variables
Error Handling
All tools automatically handle errors and return them in MCP format:TypeScript Support
Full TypeScript support with type inference:Key Points:
- Input schema is defined using
inputClassin the@Tooldecorator - Output schema is inferred from the return type
- For tools with no input parameters, omit the
inputClassoption - Use
@SchemaConstraintdecorators to add validation and documentation to your input classes
Related Packages
- @leanmcp/cli - CLI tool for creating new projects
- @leanmcp/auth - Authentication decorators and providers
- @leanmcp/utils - Utility functions