Overview
This example demonstrates how to build a sentiment analysis service using the LeanMCP SDK. You’ll learn how to:- Define type-safe input and output schemas
- Use schema constraints for validation
- Create multiple tools in a single service
- Implement simple sentiment detection logic
Complete Example
This example is from the basic-sentiment-tool in the LeanMCP repository.Define Input Schema
Create a class with schema constraints to define what inputs your tool accepts:Key points:
@SchemaConstraintadds validation rules@Optional()marks optional parametersenumrestricts values to specific optionsminLengthensures non-empty strings
Define Output Schema
Create a class that defines the structure of your tool’s response:Key points:
- Output schema ensures consistent response format
minimumandmaximumdefine valid ranges- Type safety prevents runtime errors
Implement the Service
Create your service class with the Key points:
@Tool decorator:@Tooldecorator registers the method as an MCP toolinputClassspecifies the input schema- Return type is automatically used for output schema
- Tool name is inferred from method name (“analyzeSentiment”)
Add Additional Tools
Add more tools to provide statistics and service information:Key points:
- Tools without parameters don’t need an input class
- Return types can be defined inline
- Multiple tools can coexist in one service
Project Structure
Testing the Tool
Start your server:http://localhost:8080/mcp.
Example Requests
Analyze positive text:Key Takeaways
- Type Safety: Input and output schemas provide compile-time validation
- Schema Constraints: Add validation rules directly to your classes
- Auto-Discovery: Services are automatically loaded from the
mcp/directory - Multiple Tools: One service can expose multiple related tools
- Clean Code: Decorators keep your code concise and readable
Next Steps
Weather Service
Learn how to handle optional parameters and enums
Calculator Service
See how to handle multiple operations with error handling