Service Basics
Services in LeanMCP are TypeScript classes that expose tools, prompts, and resources through decorators. Each service is automatically discovered from the./mcp directory.
Creating Your First Service
Generate a new service
Use the CLI to scaffold a new service:This creates
mcp/sentiment/index.ts with boilerplate code.Service Patterns
Multiple Tools in One Service
Organize related functionality together:mcp/sentiment/index.ts
Tools, Prompts, and Resources Together
Combine different MCP primitives in one service:mcp/weather/index.ts
Best Practices
1. Use Descriptive Names
Choose clear, action-oriented names for tools:2. Validate Input Thoroughly
Use@SchemaConstraint to enforce data quality:
3. Keep Services Focused
Each service should have a single responsibility:4. Use Shared Configuration
Create aconfig.ts for shared dependencies:
mcp/config.ts
mcp/slack/index.ts
5. Handle Errors Gracefully
Provide clear error messages:Advanced Patterns
Stateful Services
Services can maintain state across invocations:Dependency Injection
Pass dependencies through the constructor:mcp/database/index.ts
Composable Services
Services can call other services:Troubleshooting
Service Not Discovered
Problem: Your service isn’t being registered. Solutions:- Ensure the file is in the
./mcpdirectory - Export your service class:
export class MyService - Check TypeScript compilation:
npm run build - Verify no constructor parameters are required
Schema Validation Errors
Problem: Input validation is failing. Solutions:- Check
@SchemaConstraintdefinitions match your types - Use
@Optional()for optional fields - Verify enum values match exactly
- Test with valid sample data first
Decorator Not Working
Problem:@Tool, @Prompt, or @Resource decorator isn’t recognized.
Solutions:
- Enable decorators in
tsconfig.json: - Import decorators from
@leanmcp/core - Restart your TypeScript language server
Next Steps
Auth Integration
Add authentication to protect your services
Schema Design
Learn advanced schema validation patterns
Error Handling
Implement robust error handling
Deployment
Deploy your services to production