-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglean_mcp.py
More file actions
220 lines (182 loc) · 6.73 KB
/
glean_mcp.py
File metadata and controls
220 lines (182 loc) · 6.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env python3
"""Glean MCP server.
Exposes Glean's search, chat, and agent surfaces as MCP tools for use with
Claude Code, Claude Desktop, Cursor, and any other MCP-compatible client.
Setup — Claude Code (.claude/settings.json):
{
"mcpServers": {
"glean": {
"command": "python3",
"args": ["/absolute/path/to/glean-code-cli/glean_mcp.py"]
}
}
}
Setup — Claude Desktop (~/Library/Application Support/Claude/claude_desktop_config.json):
{
"mcpServers": {
"glean": {
"command": "python3",
"args": ["/absolute/path/to/glean-code-cli/glean_mcp.py"]
}
}
}
Credentials are loaded from ~/.gleancode/config.json (written by /login in
the glean-code REPL), or from environment variables:
GLEAN_INSTANCE e.g. my-company-be.glean.com
GLEAN_TOKEN Glean API bearer token
GLEAN_ACT_AS Optional email to impersonate (X-Glean-ActAs)
Requires Python 3.10+ and the mcp package:
pip install "mcp[cli]"
"""
from __future__ import annotations
import json
import os
import sys
from pathlib import Path
from typing import Optional
# Allow running directly from the repo root without a package install
_HERE = Path(__file__).parent
if str(_HERE) not in sys.path:
sys.path.insert(0, str(_HERE))
try:
from mcp.server.fastmcp import FastMCP
except ImportError:
print(
"The 'mcp' package is required to run the Glean MCP server.\n"
"Install it with: pip install \"mcp[cli]\"\n",
file=sys.stderr,
)
sys.exit(1)
from glean_code.config import Config
from glean_code.client import GleanClient, GleanError
def _build_client() -> tuple[Config, GleanClient]:
cfg = Config.load()
if os.environ.get("GLEAN_INSTANCE"):
cfg.instance = os.environ["GLEAN_INSTANCE"]
if os.environ.get("GLEAN_TOKEN"):
cfg.api_token = os.environ["GLEAN_TOKEN"]
if os.environ.get("GLEAN_ACT_AS"):
cfg.act_as = os.environ["GLEAN_ACT_AS"]
cfg.mode = "live"
return cfg, GleanClient(cfg)
_cfg, _client = _build_client()
mcp = FastMCP("glean")
# ── tools ────────────────────────────────────────────────────────────────────
@mcp.tool()
def search(
query: str,
datasource: Optional[str] = None,
page_size: int = 10,
) -> str:
"""Search the Glean index across all connected data sources.
Returns ranked results with titles, URLs, datasource names, and text
snippets. Use datasource to restrict to a single source (e.g. 'confluence',
'gdrive', 'slack', 'jira', 'github').
"""
try:
resp = _client.search(query, page_size=page_size, datasource=datasource)
except GleanError as e:
return f"Error: {e}"
results = resp.get("results", [])
if not results:
return "No results found."
total = resp.get("totalCount", len(results))
lines = [f"Search results for '{query}' ({total} total):\n"]
for i, r in enumerate(results, 1):
title = r.get("title", "(untitled)")
url = r.get("url", "")
ds = r.get("datasource", "")
snip = ""
snips = r.get("snippets") or []
if snips:
snip = snips[0].get("text", "")
lines.append(f"{i}. {title}")
meta = " " + " ".join(x for x in [ds, url] if x)
if meta.strip():
lines.append(meta)
if snip:
lines.append(f" {snip}")
lines.append("")
return "\n".join(lines).rstrip()
@mcp.tool()
def chat(
message: str,
chat_id: Optional[str] = None,
agent: Optional[str] = None,
) -> str:
"""Chat with the Glean Assistant.
Returns the assistant's response along with any cited sources.
Pass chat_id to continue an existing conversation thread — the id is
included in every response and should be forwarded on subsequent turns.
Use agent to route the message through a named agent configuration.
"""
try:
resp = _client.chat(message, chat_id=chat_id, agent=agent)
except GleanError as e:
return f"Error: {e}"
parts: list[str] = []
thread_id = resp.get("chatId")
if thread_id:
parts.append(f"[chat_id: {thread_id}]")
for msg in resp.get("messages", []):
text = "".join(f.get("text", "") for f in msg.get("fragments", []))
if text:
parts.append(text)
citations = msg.get("citations", [])
if citations:
parts.append("\nSources:")
for c in citations:
doc = c.get("sourceDocument", {})
line = f" - {doc.get('title', '?')}"
if doc.get("url"):
line += f" {doc['url']}"
parts.append(line)
return "\n".join(parts) if parts else "(no response)"
@mcp.tool()
def list_agents(query: str = "") -> str:
"""List Glean agents available to the current token.
Optionally filter by name or description with the query parameter.
The id field from each result is what run_agent expects.
"""
try:
resp = _client.agents_search(query=query or None)
except GleanError as e:
return f"Error: {e}"
agents = resp.get("agents", [])
if not agents:
return "No agents found."
lines = ["Available agents:\n"]
for a in agents:
lines.append(f" id: {a.get('id', '?')}")
if a.get("name"):
lines.append(f" name: {a['name']}")
if a.get("description"):
lines.append(f" description: {a['description']}")
lines.append("")
return "\n".join(lines).rstrip()
@mcp.tool()
def run_agent(agent_id: str, input: str) -> str:
"""Run a Glean agent and return its final output.
Blocks until the agent completes (uses the /agents/runs/wait endpoint).
Use list_agents to discover available agent IDs.
"""
try:
resp = _client.agent_run(agent_id, input)
except GleanError as e:
return f"Error: {e}"
output = resp.get("output", "")
run_id = resp.get("runId", "")
result = output if output else json.dumps(resp, indent=2)
if run_id:
result = f"[run_id: {run_id}]\n\n{result}"
return result
# ── entry point ───────────────────────────────────────────────────────────────
if __name__ == "__main__":
if not _cfg.is_live_ready:
print(
"Warning: no instance or token configured.\n"
"Set GLEAN_INSTANCE + GLEAN_TOKEN env vars, "
"or run /login inside glean-code first.",
file=sys.stderr,
)
mcp.run(transport="stdio")