A sample weather API that has been enhanced with Model Context Protocol (MCP) server capabilities, demonstrating how to convert an existing web API into an MCP server.
This project serves as a practical example of integrating MCP (Model Context Protocol) functionality into an existing .NET web API. It started as a simple weather forecast API and has been enhanced to serve as an MCP server, allowing AI agents and other clients to interact with weather data through the standardized MCP protocol.
The Model Context Protocol (MCP) is an open standard that enables seamless integration between AI applications and external data sources. MCP allows AI models to securely connect to and interact with various tools, databases, and services through a standardized interface.
Key benefits of MCP:
- Standardized Communication: Provides a consistent way for AI agents to interact with tools and data sources
- Security: Ensures secure and controlled access to external resources
- Flexibility: Supports various transport mechanisms (HTTP, WebSocket, etc.)
- Tool Integration: Enables AI agents to call functions and access data from external services
This project demonstrates the minimal changes needed to add MCP capabilities to an existing ASP.NET Core web API:
Added the MCP server package to WeatherMcp.csproj:
<PackageReference Include="ModelContextProtocol.AspNetCore" Version="0.2.0-preview.3" />Created Tools/WeatherTools.cs with MCP-decorated methods:
[McpServerToolType]attribute on the class[McpServerTool]attribute on public methods[Description]attributes for documentation
In Program.cs, added MCP server services:
// Add MCP server with HTTP transport
builder.Services.AddMcpServer()
.WithHttpTransport()
.WithToolsFromAssembly();
// Map MCP endpoints
app.MapMcp();The weather tools wrap the existing weather service, making it accessible via MCP:
GetWeatherForecast()- Get forecast for default cityGetWeatherForecastForCity(string city)- Get forecast for specific city
- .NET 9 SDK
- Visual Studio Code (recommended) or Visual Studio
# Build the solution
dotnet build
# Run the application
dotnet run --project WeatherMcp
# Or run with watch for development
dotnet watch --project WeatherMcpThe application will start on http://localhost:5260 (or similar port).
REST API Endpoints:
GET /weatherforecast- Get 5-day forecast for default cityGET /weatherforecast/{city}- Get 5-day forecast for specific cityGET /swagger- Swagger UI documentation
You can test these endpoints using the WeatherMcp.http file. Example:
### Get weather forecast for London
GET http://localhost:5260/weatherforecast/London
Accept: application/jsonMCP Endpoints:
POST /- MCP JSON-RPC endpoint for all MCP communication
Test MCP endpoints using WeatherMcp.mcp.http. Example:
### Call GetWeatherForecastForCity Tool
POST http://localhost:5260/
Content-Type: application/json
{
"jsonrpc": "2.0",
"id": 5,
"method": "tools/call",
"params": {
"name": "GetWeatherForecastForCity",
"arguments": {
"city": "Amsterdam"
}
}
}You can use the MCP Inspector to interact with your MCP server:
npx @modelcontextprotocol/inspectorThis provides a web-based interface to test MCP tools and explore the server capabilities.
To connect this MCP server to Claude Desktop, add the following to your claude_desktop_config.json file:
{
"mcpServers": {
"weather": {
"command": "npx",
"args": [
"-y",
"mcp-remote",
"http://localhost:5260"
]
}
}
}Run the comprehensive test suite:
# Run all tests
dotnet test
# Run tests with verbose output
dotnet test --verbosity normal
# Run specific test project
dotnet test WeatherMcp.TestsThe project includes 22 tests covering:
- Weather service functionality
- REST API endpoints
- MCP server configuration
- Weather tools integration
Use the provided HTTP file to test REST endpoints:
# Using VS Code REST Client extension or similar tools
# Open and run requests from: WeatherMcp/WeatherMcp.httpExample requests:
- Get default weather forecast
- Get weather for London, New York, Tokyo
- Access Swagger documentation
Test MCP functionality using the MCP HTTP file:
# Open and run requests from: WeatherMcp/WeatherMcp.mcp.httpMCP test scenarios:
- Initialize MCP session
- List available tools
- Call
GetWeatherForecasttool - Call
GetWeatherForecastForCitytool with parameters
This repository includes a Dev Container configuration that provides a consistent development environment with .NET 9 pre-installed. This is particularly useful for GitHub Copilot coding agents and ensures a smooth development experience.
- Open this repository in VS Code
- Install the "Dev Containers" extension
- Press
Ctrl+Shift+Pand select "Dev Containers: Reopen in Container" - The container will automatically set up .NET 9 and all necessary tools
For more details about the dev container setup, see .devcontainer/README.md.
- Setup: Use the dev container for consistent environment
- Build:
dotnet buildto compile the solution - Test:
dotnet testto run unit tests - Run:
dotnet watch --project WeatherMcpfor development with hot reload - HTTP Testing: Use the
.httpfiles to test both REST and MCP endpoints
WeatherMcp/
├── Models/ # Data models (WeatherForecast)
├── Services/ # Business logic (WeatherService)
├── Tools/ # MCP tools (WeatherTools)
├── Program.cs # Application startup and configuration
├── WeatherMcp.http # REST API test requests
└── WeatherMcp.mcp.http # MCP protocol test requests
WeatherMcp.Tests/ # Unit tests for all components
This example demonstrates how straightforward it can be to add MCP capabilities to existing APIs, enabling them to be consumed by AI agents and other MCP-compatible clients.