Overview
This example demonstrates how to build a complete weather service using the LeanMCP SDK. You’ll learn how to:- Create tools that fetch data
- Define prompts for LLM interactions
- Expose resources for static data
- Handle optional parameters with enums
- Use multiple decorators in one service
Complete Example
This example is based on the weather service pattern from the LeanMCP README.Define Input Schema
Create input classes with optional parameters and enums:Key points:
@Optional()makes the units parameter optionalenumrestricts values to specific optionsdefaultprovides a fallback value
Define Output Schema
Create a structured output class with constraints:Key points:
enumensures valid condition valuesminimumandmaximumvalidate humidity range- Descriptive constraints improve AI agent understanding
Implement the Tool
Create a tool to fetch current weather:Key points:
- Tool handles optional
unitsparameter - Private helper method simulates API call
- Type safety ensures correct data structure
Add a Prompt
Create a prompt template for weather queries:Key points:
- Prompts help standardize LLM interactions
- Return structured messages with role and content
- Optional parameters allow flexible prompts
Add a Resource
Expose static data through resources:Key points:
- Resources provide read-only data
- Useful for configuration and metadata
- No input parameters needed for simple resources
Project Structure
Testing the Service
Start your server:Example Tool Calls
Get weather in metric units:Example Prompt Call
Example Resource Call
Integrating Real APIs
To connect to a real weather API like OpenWeatherMap:Key Takeaways
- Tools, Prompts, Resources: One service can use all three MCP primitives
- Optional Parameters: Use
@Optional()anddefaultvalues - Enums: Restrict values to valid options
- Type Safety: Schemas prevent invalid data
- API Integration: Easy to connect to external services
Next Steps
Calculator Service
Learn error handling and multiple operations
Authentication
Add authentication to your services