Skip to main content
Glama

DotNet MCP Server Demo

by kasirajan22

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 messages

  • GET /api/mcp/tools - Get available tools

  • GET /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

  1. get_time: Returns the current date and time

  2. echo: Echoes back the provided message

  3. 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:

  1. Use proper user authentication with a database

  2. Implement secure password hashing

  3. Use HTTPS only

  4. Consider JWT tokens instead of Basic Auth

  5. Implement proper authorization policies

  6. Add rate limiting and input validation

Extending the Server

Adding New Tools

  1. Add the tool definition in McpServer.GetToolsAsync()

  2. Implement the tool logic in McpServer.ExecuteToolAsync()

  3. Define the input schema for the tool

Adding New Resources

  1. Add resource definitions in McpServer.GetResourcesAsync()

  2. 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.

-
security - not tested
F
license - not found
-
quality - not tested

Related MCP Servers

  • -
    security
    -
    license
    -
    quality
    This 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
  • -
    security
    A
    license
    -
    quality
    A 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 -
    130
    1,461
    MIT License
    • Apple
    • Linux
  • -
    security
    -
    license
    -
    quality
    A 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.
  • A
    security
    A
    license
    A
    quality
    A Model Context Protocol server for time manipulation tasks, enabling AI models to get the current date/time and calculate duration between timestamps.
    Last updated -
    7
    2
    MIT License
    • Apple

View all related MCP servers

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/kasirajan22/DotnetMCPServer'

If you have feedback or need assistance with the MCP directory API, please join our Discord server