MCP Demo - .NET Web API with Model Context Protocol Server
This project demonstrates a simple .NET 8 Web API that implements a Model Context Protocol (MCP) server with Basic Authentication.
Features
- MCP Server Implementation: Handles MCP protocol messages including initialization, tool calls, and resource management
- Basic Authentication: Simple username/password authentication for API endpoints
- Sample Tools: Includes example tools like time retrieval, echo, and calculator
- RESTful API: Standard REST endpoints for MCP operations
- Swagger Documentation: Auto-generated API documentation
Prerequisites
- .NET 8.0 SDK or later
- Visual Studio 2022, VS Code, or any preferred IDE
Getting Started
1. Restore Dependencies
dotnet restore
2. Build the Project
dotnet build
3. Run the Application
dotnet run
The application will start on https://localhost:5001 and http://localhost:5000.
4. Access Swagger UI
Open your browser and navigate to: https://localhost:5001/swagger
Authentication
The API uses Basic Authentication with hardcoded credentials for demonstration:
- Username:
admin - Password:
password123
Using Basic Auth in HTTP Requests
Include the Authorization header with Base64 encoded credentials:
Authorization: Basic YWRtaW46cGFzc3dvcmQxMjM=
API Endpoints
MCP Endpoints (Require Authentication)
POST /api/mcp/message- Handle MCP protocol messagesGET /api/mcp/tools- Get available toolsGET /api/mcp/resources- Get available resources
Health Check (Public)
GET /api/mcp/health- Health check endpoint
Sample Endpoints
GET /api/weather- Get weather data (requires authentication)GET /api/weather/public- Get public weather data (no authentication)
MCP Protocol Usage
Initialize the Server
POST /api/mcp/message
{
"jsonrpc": "2.0",
"id": "1",
"method": "initialize",
"params": {
"protocolVersion": "2024-11-05",
"capabilities": {},
"clientInfo": {
"name": "Test Client",
"version": "1.0.0"
}
}
}
List Available Tools
POST /api/mcp/message
{
"jsonrpc": "2.0",
"id": "2",
"method": "tools/list"
}
Call a Tool
POST /api/mcp/message
{
"jsonrpc": "2.0",
"id": "3",
"method": "tools/call",
"params": {
"name": "echo",
"arguments": {
"message": "Hello, MCP!"
}
}
}
Calculate Tool Example
POST /api/mcp/message
{
"jsonrpc": "2.0",
"id": "4",
"method": "tools/call",
"params": {
"name": "calculate",
"arguments": {
"operation": "add",
"a": 10,
"b": 5
}
}
}
Available Tools
- get_time: Returns the current date and time
- echo: Echoes back the provided message
- calculate: Performs basic arithmetic operations (add, subtract, multiply, divide)
Project Structure
MCPDemo/
├── Controllers/
│ ├── McpController.cs # MCP protocol endpoints
│ └── WeatherController.cs # Sample API endpoints
├── Models/
│ └── McpModels.cs # MCP protocol data models
├── Services/
│ ├── BasicAuthenticationHandler.cs # Basic auth implementation
│ └── McpServer.cs # MCP server logic
├── Program.cs # Application entry point
├── appsettings.json # Configuration
└── MCPDemo.csproj # Project file
Security Considerations
⚠️ Important: This implementation uses hardcoded credentials for demonstration purposes only. In production:
- Use proper user authentication with a database
- Implement secure password hashing
- Use HTTPS only
- Consider JWT tokens instead of Basic Auth
- Implement proper authorization policies
- Add rate limiting and input validation
Extending the Server
Adding New Tools
- Add the tool definition in
McpServer.GetToolsAsync() - Implement the tool logic in
McpServer.ExecuteToolAsync() - Define the input schema for the tool
Adding New Resources
- Add resource definitions in
McpServer.GetResourcesAsync() - Implement resource reading logic in
HandleResourceReadAsync()
Testing with curl
Health Check
curl -X GET https://localhost:5001/api/mcp/health
Get Tools (with auth)
curl -X GET https://localhost:5001/api/mcp/tools -H "Authorization: Basic YWRtaW46cGFzc3dvcmQxMjM="
Send MCP Message
curl -X POST https://localhost:5001/api/mcp/message `
-H "Content-Type: application/json" `
-H "Authorization: Basic YWRtaW46cGFzc3dvcmQxMjM=" `
-d '{
"jsonrpc": "2.0",
"id": "1",
"method": "initialize",
"params": {
"protocolVersion": "2024-11-05",
"capabilities": {},
"clientInfo": {"name": "Test Client", "version": "1.0.0"}
}
}'
License
This project is for demonstration purposes only.
