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
2. Build the Project
3. Run the Application
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:
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
List Available Tools
Call a Tool
Calculate Tool Example
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
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
Get Tools (with auth)
Send MCP Message
License
This project is for demonstration purposes only.
This server cannot be installed
remote-capable server
The server can be hosted and run remotely because it primarily relies on remote services or has no dependency on the local environment.
A demonstration .NET 8 Web API that implements the Model Context Protocol with basic authentication. Provides sample tools for time retrieval, echo functionality, and basic arithmetic calculations through RESTful endpoints.
Related MCP Servers
- -securityFlicense-qualityThis server enables running a Model Context Protocol in a web browser with functionalities including arithmetic operations and session-based key-value storage.Last updated -26
- -securityAlicense-qualityA Model Context Protocol server that enables AI assistants to interact with n8n workflows through natural language, supporting actions like listing, creating, updating, executing and monitoring workflows.Last updated -1771,314MIT License
- -securityAlicense-qualityA Model Context Protocol server that enables AI assistants to interact with n8n workflows through natural language, providing access to n8n's complete API functionality including workflow management, user administration, and credential handling.Last updated -95MIT License
- AsecurityAlicenseAqualityA Model Context Protocol server for time manipulation tasks, enabling AI models to get the current date/time and calculate duration between timestamps.Last updated -72MIT License