Skip to content

MCP Server Integration

The Model Context Protocol (MCP) implementation in glean-powershell provides a PowerShell-native bridge between AI clients and the Glean platform, allowing models to query knowledge bases, chat, and read documents. The server operates as a JSON-RPC 2.0 service over standard I/O, exposing a curated set of tools that replicate the functionality of the official Glean MCP server while supporting both token-based and cookie-based authentication modes.

The MCP server is initiated via the Start-GleanMcpServer function, which serves as the primary entrypoint for the module. This function is designed to be invoked directly by AI coding assistants, such as Claude Code, using a command like claude mcp add glean -- pwsh -NoProfile -File /path/to/mcp/Start-GleanMcpServer.ps1 1. The script mcp/Start-GleanMcpServer.ps1 acts as the launcher, importing the core GleanMcp.psm1 module and calling the server start function.

The server communicates exclusively over standard I/O using newline-delimited JSON-RPC messages. To ensure protocol integrity, all logging and verbose output are directed to stderr, while only valid JSON-RPC responses are written to stdout . The server loop reads lines from stdin, parses them as JSON, and dispatches them to the request handler .

diagram

The server supports two authentication modes determined by the GLEAN_AUTH_MODE environment variable or explicit parameters: token-based and cookie-based.

In token mode (the default), the server uses GLEAN_INSTANCE or GLEAN_BASE_URL along with GLEAN_API_TOKEN to establish a connection via Connect-Glean . This mode relies on the standard PowerShell module cmdlets for API interaction .

In cookie mode, the server attempts to reuse an existing Single Sign-On (SSO) session stored by a prior interactive Connect-Glean -CookieAuth command . It validates the session using Test-GleanConnection before proceeding; if the session is missing or expired, the server exits with a clear error message instructing the user to re-sync their authentication . This mode routes requests through Invoke-GleanWebRequest to interact with the web-client API endpoints .

The server exposes a fixed set of tools via the tools/list JSON-RPC method, defined in Get-GleanMcpTools . These tools are implemented in Invoke-GleanMcpTool, which routes calls to either the typed REST cmdlets (token mode) or the web-client API (cookie mode) .

The core tools include:

  • glean_search: Searches the company knowledge base and returns ranked results including title, URL, and snippet .
  • glean_chat: Sends a message to Glean Assistant and returns the grounded answer .
  • glean_read_document: Fetches the content or metadata of a specific document by ID or URL .
  • glean_people_search: Searches the company directory for people or teams .

Additionally, an advanced tool named glean_invoke is available if the environment variable GLEAN_MCP_ALLOW_INVOKE is set to 1 . This tool allows the AI client to invoke any cmdlet from the Glean module by name, passing a JSON body as parameters . This provides access to the full 112-operation surface of the module but is disabled by default to prevent unintended side effects 2 .

The core logic for processing JSON-RPC requests is encapsulated in New-GleanMcpResponse, which is a pure function designed for unit testing . It handles standard MCP methods such as initialize, ping, tools/list, and tools/call .

When a tool is called via tools/call, the server executes the corresponding logic in Invoke-GleanMcpTool and formats the output using specific helper functions:

  • Format-GleanSearchText: Converts search results into a readable text format with title, URL, and snippet .
  • Format-GleanChatText: Extracts the text fragments from the chat response .
  • ConvertTo-Json: Used for document and people search results to return structured JSON data .

Errors are caught and returned as JSON-RPC responses with isError set to true and a descriptive message in the content text .