MCP-Server-for-Excel-Integration

MCP-Server-for-Excel-Integration

0

The MCP Server is a FastAPI-based application designed to process lists of numerical inputs by doubling their values. It features session management and API key authentication, making it suitable for applications requiring secure and stateful API use.

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