Skip to main content
Glama
ashishsurve353

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"
    }
  }
}

```

---


```

```