Model Context Protocol (MCP) Server

by AyushRatan1

305 downloads
Not rated
GitHub

Description

# Model Context Protocol (MCP) Server A simple MCP server built with FastAPI that processes numerical inputs by doubling them, with support for session management and API key authentication. ## Setup 1. Install the required dependencies: ```bash pip install -r requirements.txt…

About

# Model Context Protocol (MCP) Server A simple MCP server built with FastAPI that processes numerical inputs by doubling them, with support for session management and API key authentication. ## Setup 1. Install the required dependencies: ```bash pip install -r requirements.txt ``` 2. Start the server: ```bash uvicorn…

Details

Author
AyushRatan1
Downloads
305
Categories
Other, API

- Doubles each numerical input value
- Session management via optional session_id header
- API key authentication with a demo key
- Excel VBA integration example provided
- CORS middleware for cross-origin requests
- FastAPI-based, simple REST API

Install dependencies with pip install -r requirements.txt, then start the server with uvicorn main:app --reload. Send a POST request to http://localhost:8000/mcp/process with a JSON body containing an inputs array of numbers. Optionally include session_id and api_key headers (demo key: "demo_key").

Model Context Protocol (MCP) Server

A simple MCP server built with FastAPI that processes numerical inputs by doubling them, with support for session management and API key authentication.

Setup

1. Install the required dependencies:

pip install -r requirements.txt

2. Start the server:

uvicorn main:app --reload

The server will be running at http://localhost:8000

API Endpoints

POST /mcp/process

Processes a list of numerical inputs by doubling each value.

Request Body:

{
"inputs": [1.0, 2.0, 3.0]
}

Headers (optional):
- session_id: To maintain state between requests
- api_key: For authentication (demo key: "demo_key")

Response:

{
"outputs": [2.0, 4.0, 6.0],
"session_id": "generated-session-id"
}

Excel VBA Integration

You can use the following VBA code to integrate with the MCP server from Excel:

Sub CallMCPServer()
    ' Define the input range
    Dim inputRange As Range
    Set inputRange = Range("A1:A3")
    
    ' Create an HTTP request
    Dim request As Object
    Set request = CreateObject("MSXML2.XMLHTTP")
    
    ' Prepare JSON data
    Dim jsonData As String
    jsonData = "{""inputs"": ["
    
    Dim cell As Range
    Dim first As Boolean
    first = True
    
    For Each cell In inputRange
        If Not first Then
            jsonData = jsonData & ", "
        End If
        jsonData = jsonData & cell.Value
        first = False
    Next cell
    
    jsonData = jsonData & "]}"
    
    ' Send the request
    request.Open "POST", "http://localhost:8000/mcp/process", False
    request.setRequestHeader "Content-Type", "application/json"
    request.send jsonData
    
    ' Handle the response
    If request.Status = 200 Then
        Dim response As Object
        Set response = ParseJson(request.responseText)
        
        ' Display the outputs
        Dim outputStr As String
        outputStr = "Processed outputs: " & vbCrLf
        
        Dim i As Integer
        For i = 1 To response.Item("outputs").Count
            outputStr = outputStr & response.Item("outputs").Item(i) & vbCrLf
        Next i
        
        MsgBox outputStr, vbInformation, "MCP Server Response"
        
        ' Optionally store the session ID for future requests
        Dim sessionId As String
        sessionId = response.Item("session_id")
    Else
        MsgBox "Error: " & request.Status & " - " & request.responseText, vbExclamation
    End If
End Sub

Function ParseJson(jsonString As String) As Object
' This is a simplified JSON parser for demo purposes
' For production use, consider a proper JSON library
Dim ScriptEngine As Object
Set ScriptEngine = CreateObject("ScriptControl")
ScriptEngine.Language = "JScript"

Dim JsonCode As String
JsonCode = "JSON.parse('" & Replace(jsonString, "'", "\'") & "');"

Set ParseJson = ScriptEngine.Eval(JsonCode)
End Function

For modern Excel with Office Scripts, you can use JavaScript/TypeScript with fetch API to make HTTP requests to the MCP server.

Notes

- The server includes CORS middleware to allow cross-origin requests from web-based Excel clients
- Session state is stored in memory and will be lost if the server restarts
- For production use, consider using a database for session storage and proper authentication

No reviews yet — be the first

Sign in to leave a review

Use Google, GitHub, or an email account so ratings stay tied to real people.

Email sign in

No reviews posted yet.