sharepoint-mcp-server
README.md
Here is the complete text formatted inside a single code block. You can click the **"Copy"** button at the top-right corner of the block to copy everything in one click:
```markdown
# SharePoint MCP Server & AI Orchestrator (.NET 9)
An end-to-end local AI-driven SharePoint orchestrator that connects a local LLM (**Ollama**) to Microsoft SharePoint via the **Model Context Protocol (MCP)** and **Microsoft Graph API**.
---
## ๐๏ธ Architecture Overview
```
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 1. Natural Language Prompt โโโโโโโโโโโโโโโโโโโโโโโโโ
โ Console AI Orchestrator โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ>โ Ollama (qwen2.5:1.5b) โ
โ (SharePointMcpClient) โ <โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ (Local GPU Inference) โ
โโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโ 2. Tool Call Request (JSON) โโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โ 3. Execute Tool via MCP (`CallToolAsync`)
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ C# SharePoint MCP Server โ โโโ> Microsoft Graph API โโโ> SharePoint Online
โ (SharePointMcpServer Web API)โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
```
---
## ๐ Repository Structure
```text
.
โโโ SharePointMcpServer/ # ASP.NET Core Web API implementing MCP Server tools
โ โโโ Controllers/ # MCP endpoints (/mcp)
โ โโโ Services/ # SharePoint Graph API service methods
โ โโโ appsettings.json # Azure Entra ID configuration
โ
โโโ SharePointMcpClient/ # .NET 9 Console App (AI Orchestrator)
โโโ Program.cs # Interactive CLI with automatic tool invocation
```
---
## ๐ Prerequisites
* **[.NET 9 SDK](https://dotnet.microsoft.com/download/dotnet/9.0)**
* **[Ollama](https://ollama.com/)** installed locally
* **Microsoft Entra ID (Azure AD)** Tenant with Application Permissions to SharePoint/Graph API
---
## โ๏ธ Setup & Configuration
### 1. Microsoft Entra ID (Azure AD) Setup
1. Go to the **Microsoft Entra Admin Center** (`https://entra.microsoft.com/`).
2. Navigate to **Identity** -> **Applications** -> **App registrations** -> **New registration**.
3. Name your application (e.g., `SharePoint-MCP-Server`) and select **Single Tenant**.
4. Note the **Application (Client) ID** and **Directory (Tenant) ID**.
5. Under **Certificates & secrets**, generate a new **Client Secret** and copy its **Value**.
6. Under **API permissions**, add the following **Microsoft Graph Application Permissions**:
* `Sites.Read.All`
* `Files.Read.All`
7. Click **Grant admin consent for [Your Organization]**.
---
### 2. Configure Server (`SharePointMcpServer`)
Open `SharePointMcpServer/appsettings.json` and enter your Azure AD credentials:
``` json
"AzureEntra": {
"Instance": "https://login.microsoftonline.com/",
"TenantId": "",
"ClientId": "",
"ClientSecret": "",
"Scopes": [ "https://graph.microsoft.com/.default" ]
},
```
---
### 3. Setup Ollama Model
Pull the recommended lightweight model for fast local tool execution (~1.2 GB VRAM footprint):
```bash
ollama pull qwen2.5:1.5b
```
> **Tip (Disk Space Optimization):** To store Ollama models on a secondary drive (e.g., `D:\OllamaModels`), set the environment variable:
> ```cmd
> setx OLLAMA_MODELS "D:\OllamaModels"
>
> ```
>
>
---
## ๐ Running the Solution
### Step 1: Start the MCP Server
Navigate to the server directory and run:
```bash
cd SharePointMcpServer
dotnet run
```
*The server will start listening at `https://localhost:7149` (or your configured port).*
---
### Step 2: Start the AI Orchestrator Console App
In a separate terminal, navigate to the client directory and run:
```bash
cd SharePointMcpClient
dotnet run
```
---
## ๐ฌ Usage Flow
1. **Domain Onboarding:** When prompted, enter your SharePoint tenant URL (e.g., `https://yourtenant.sharepoint.com/`).
2. **Site Selection:** The console runner calls `list_sites` directly and displays an interactive menu of all accessible sites.
3. **Natural Language Querying:** Select a site index (e.g., `1`), then ask questions naturally without manually supplying site IDs:
```text
[Main Site] User > list libraries
[Main Site] User > show recent documents
[Main Site] User > search documents for 'invoice'
```
---
## ๐ ๏ธ Key Technical Features
* **Interactive Site Context Management:** Automatically discovers tenant sites and retains active `siteId` context across multi-turn user queries.
* **Middleware Integration:** Utilizes `Microsoft.Extensions.AI` with `.UseFunctionInvocation()` middleware for automatic two-way execution between Ollama and MCP tools.
* **Defensive Parameter Normalization:**
* Handles key casing remappings (e.g., `site_id` -> `siteId`).
* Unwraps `JsonElement` primitive wrappers.
* Employs Regex lookahead sanitization to clean composite site IDs without corrupting server-relative URL paths.
* **Deterministic Tool Binding:** Configured with `Temperature = 0.0f` and strict context limits for accurate schema mapping.
---
## ๐งช Testing Endpoints Directly via Postman
You can test MCP endpoints directly via HTTP POST to `https://localhost:7149/mcp`:
**Payload Example (`list_libraries`):**
```json
{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "list_libraries",
"arguments": {
"siteId": "yourtenant.sharepoint.com,guid-1,guid-2"
}
}
}
```
---
```
```This server cannot be deployed
Maintenance
ActivitySlowing
ResponsivenessNo issues