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|
+
"""MCP server exposing GitHub tooling for ChatGPT apps."""
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
+
from typing import Any, Dict, List, Optional
|
| 5 |
+
|
| 6 |
+
from mcp.server.fastmcp import FastMCP
|
| 7 |
+
|
| 8 |
+
from github_client import GitHubAPIError, GitHubClient
|
| 9 |
+
|
| 10 |
+
server = FastMCP("github-gpt-tools")
|
| 11 |
+
_client = GitHubClient()
|
| 12 |
+
|
| 13 |
+
HTML_MIME_TYPE = "text/html+skybridge"
|
| 14 |
+
RESOURCE_URIS = {
|
| 15 |
+
"repo_info": "ui://github/repo_info.html",
|
| 16 |
+
"issues": "ui://github/issues.html",
|
| 17 |
+
"pulls": "ui://github/pulls.html",
|
| 18 |
+
"readme": "ui://github/readme.html",
|
| 19 |
+
"contributors": "ui://github/contributors.html",
|
| 20 |
+
"files": "ui://github/files.html",
|
| 21 |
+
"search": "ui://github/search.html",
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def _tool_meta(resource_uri: str) -> Dict[str, Any]:
|
| 26 |
+
return {
|
| 27 |
+
"openai/outputTemplate": resource_uri,
|
| 28 |
+
"openai/resultCanProduceWidget": True,
|
| 29 |
+
"openai/widgetAccessible": True,
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
@server.resource(
|
| 34 |
+
RESOURCE_URIS["repo_info"],
|
| 35 |
+
title="Repository Overview",
|
| 36 |
+
description="Summarize repository metadata with links and stats.",
|
| 37 |
+
mime_type=HTML_MIME_TYPE,
|
| 38 |
+
)
|
| 39 |
+
def repo_info_resource() -> str:
|
| 40 |
+
return """
|
| 41 |
+
<div id="repo-info"></div>
|
| 42 |
+
<script>
|
| 43 |
+
let lastRepo;
|
| 44 |
+
const PATHS = [
|
| 45 |
+
["toolResult"],
|
| 46 |
+
["toolOutput","result"],
|
| 47 |
+
["toolOutput"],
|
| 48 |
+
["result"],
|
| 49 |
+
["payload","data"],
|
| 50 |
+
["payload"],
|
| 51 |
+
["globals","toolOutput","result"],
|
| 52 |
+
["globals","toolOutput"],
|
| 53 |
+
["globals","widget","props","result"],
|
| 54 |
+
];
|
| 55 |
+
function pick(data) {
|
| 56 |
+
if (!data || typeof data !== "object") return undefined;
|
| 57 |
+
for (const path of PATHS) {
|
| 58 |
+
let node = data;
|
| 59 |
+
for (const key of path) {
|
| 60 |
+
if (!node || typeof node !== "object") {
|
| 61 |
+
node = undefined;
|
| 62 |
+
break;
|
| 63 |
+
}
|
| 64 |
+
node = node[key];
|
| 65 |
+
}
|
| 66 |
+
if (node) return node;
|
| 67 |
+
}
|
| 68 |
+
return undefined;
|
| 69 |
+
}
|
| 70 |
+
function handle(detail) {
|
| 71 |
+
const candidate = pick(detail) ?? pick(detail?.globals);
|
| 72 |
+
if (candidate) {
|
| 73 |
+
lastRepo = candidate;
|
| 74 |
+
return true;
|
| 75 |
+
}
|
| 76 |
+
return false;
|
| 77 |
+
}
|
| 78 |
+
function updateFromEvent(event) {
|
| 79 |
+
if (!event) return false;
|
| 80 |
+
return handle(event.detail || {});
|
| 81 |
+
}
|
| 82 |
+
function captureInitial() {
|
| 83 |
+
return handle(window.openai || {});
|
| 84 |
+
}
|
| 85 |
+
function currentRepo() {
|
| 86 |
+
if (lastRepo === undefined) {
|
| 87 |
+
if (!captureInitial()) {
|
| 88 |
+
lastRepo = {};
|
| 89 |
+
}
|
| 90 |
+
}
|
| 91 |
+
return lastRepo || {};
|
| 92 |
+
}
|
| 93 |
+
function formatNumber(value) {
|
| 94 |
+
if (typeof value !== "number") return value ?? "0";
|
| 95 |
+
return value.toLocaleString();
|
| 96 |
+
}
|
| 97 |
+
function render() {
|
| 98 |
+
const repo = currentRepo();
|
| 99 |
+
const root = document.getElementById("repo-info");
|
| 100 |
+
if (!root) return;
|
| 101 |
+
if (!repo.full_name) {
|
| 102 |
+
root.innerHTML = "<p>No repository selected yet.</p>";
|
| 103 |
+
return;
|
| 104 |
+
}
|
| 105 |
+
root.innerHTML = `
|
| 106 |
+
<h2 style="margin-bottom:0.4rem;">${repo.full_name}</h2>
|
| 107 |
+
<p style="margin:0 0 0.5rem;">${repo.description || "No description provided."}</p>
|
| 108 |
+
<div style="display:flex;gap:1rem;font-size:14px;margin:0.5rem 0 1rem;">
|
| 109 |
+
<span>⭐ ${formatNumber(repo.stargazers_count || 0)}</span>
|
| 110 |
+
<span>🍴 ${formatNumber(repo.forks_count || 0)}</span>
|
| 111 |
+
<span>❗ ${formatNumber(repo.open_issues_count || 0)}</span>
|
| 112 |
+
</div>
|
| 113 |
+
<div style="display:flex;flex-wrap:wrap;gap:0.4rem;margin-bottom:0.75rem;">
|
| 114 |
+
${(repo.topics || []).map(topic => `<span style="padding:2px 8px;border-radius:999px;background:#f1f5f9;font-size:12px;">${topic}</span>`).join("")}
|
| 115 |
+
</div>
|
| 116 |
+
<a href="${repo.html_url}" target="_blank" style="font-weight:500;">Open on GitHub ↗</a>
|
| 117 |
+
`;
|
| 118 |
+
}
|
| 119 |
+
render();
|
| 120 |
+
const eventNames = ["openai:tool_result","openai:set_globals","openai:tool_output","openai:result","openai:response"];
|
| 121 |
+
for (const name of eventNames) {
|
| 122 |
+
window.addEventListener(name, (event) => {
|
| 123 |
+
const changed = updateFromEvent(event);
|
| 124 |
+
if (changed) render();
|
| 125 |
+
});
|
| 126 |
+
}
|
| 127 |
+
</script>
|
| 128 |
+
"""
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
@server.resource(
|
| 132 |
+
RESOURCE_URIS["issues"],
|
| 133 |
+
title="Issue List",
|
| 134 |
+
description="Show currently fetched GitHub issues.",
|
| 135 |
+
mime_type=HTML_MIME_TYPE,
|
| 136 |
+
)
|
| 137 |
+
def issues_resource() -> str:
|
| 138 |
+
return """
|
| 139 |
+
<div id="issues-list"></div>
|
| 140 |
+
<script>
|
| 141 |
+
let lastIssues;
|
| 142 |
+
const PATHS = [
|
| 143 |
+
["toolResult"],
|
| 144 |
+
["toolOutput","result"],
|
| 145 |
+
["toolOutput"],
|
| 146 |
+
["result"],
|
| 147 |
+
["payload","data"],
|
| 148 |
+
["payload"],
|
| 149 |
+
["globals","toolOutput","result"],
|
| 150 |
+
["globals","toolOutput"],
|
| 151 |
+
["globals","widget","props","result"],
|
| 152 |
+
];
|
| 153 |
+
function pick(data) {
|
| 154 |
+
if (!data || typeof data !== "object") return undefined;
|
| 155 |
+
for (const path of PATHS) {
|
| 156 |
+
let node = data;
|
| 157 |
+
for (const key of path) {
|
| 158 |
+
if (!node || typeof node !== "object") {
|
| 159 |
+
node = undefined;
|
| 160 |
+
break;
|
| 161 |
+
}
|
| 162 |
+
node = node[key];
|
| 163 |
+
}
|
| 164 |
+
if (node !== undefined) return node;
|
| 165 |
+
}
|
| 166 |
+
return undefined;
|
| 167 |
+
}
|
| 168 |
+
function handle(detail) {
|
| 169 |
+
const candidate = pick(detail) ?? pick(detail?.globals);
|
| 170 |
+
if (candidate !== undefined) {
|
| 171 |
+
if (Array.isArray(candidate)) {
|
| 172 |
+
lastIssues = candidate;
|
| 173 |
+
} else if (candidate) {
|
| 174 |
+
lastIssues = [candidate];
|
| 175 |
+
} else {
|
| 176 |
+
lastIssues = [];
|
| 177 |
+
}
|
| 178 |
+
return true;
|
| 179 |
+
}
|
| 180 |
+
return false;
|
| 181 |
+
}
|
| 182 |
+
function updateFromEvent(event) {
|
| 183 |
+
if (!event) return false;
|
| 184 |
+
return handle(event.detail || {});
|
| 185 |
+
}
|
| 186 |
+
function captureInitial() {
|
| 187 |
+
return handle(window.openai || {});
|
| 188 |
+
}
|
| 189 |
+
function currentIssues() {
|
| 190 |
+
if (lastIssues === undefined) {
|
| 191 |
+
if (!captureInitial()) {
|
| 192 |
+
lastIssues = [];
|
| 193 |
+
}
|
| 194 |
+
}
|
| 195 |
+
if (Array.isArray(lastIssues)) return lastIssues;
|
| 196 |
+
if (lastIssues && typeof lastIssues === "object") return [lastIssues];
|
| 197 |
+
return [];
|
| 198 |
+
}
|
| 199 |
+
function render() {
|
| 200 |
+
const items = currentIssues();
|
| 201 |
+
const root = document.getElementById("issues-list");
|
| 202 |
+
if (!root) return;
|
| 203 |
+
if (!items.length) {
|
| 204 |
+
root.innerHTML = "<p>No issues returned.</p>";
|
| 205 |
+
return;
|
| 206 |
+
}
|
| 207 |
+
let html = "<h3>Issues</h3><ul style='margin:0;padding-left:1.2rem;'>";
|
| 208 |
+
for (const issue of items) {
|
| 209 |
+
html += `
|
| 210 |
+
<li style="margin-bottom:0.4rem;">
|
| 211 |
+
<a href="${issue.html_url}" target="_blank">
|
| 212 |
+
#${issue.number} — ${issue.title}
|
| 213 |
+
</a>
|
| 214 |
+
<div style="font-size:12px;color:#475569;">${issue.state} · ${issue.author || "unknown"}</div>
|
| 215 |
+
</li>
|
| 216 |
+
`;
|
| 217 |
+
}
|
| 218 |
+
html += "</ul>";
|
| 219 |
+
root.innerHTML = html;
|
| 220 |
+
}
|
| 221 |
+
render();
|
| 222 |
+
const eventNames = ["openai:tool_result","openai:set_globals","openai:tool_output","openai:result","openai:response"];
|
| 223 |
+
for (const name of eventNames) {
|
| 224 |
+
window.addEventListener(name, (event) => {
|
| 225 |
+
const changed = updateFromEvent(event);
|
| 226 |
+
if (changed) render();
|
| 227 |
+
});
|
| 228 |
+
}
|
| 229 |
+
</script>
|
| 230 |
+
"""
|
| 231 |
+
|
| 232 |
+
|
| 233 |
+
@server.resource(
|
| 234 |
+
RESOURCE_URIS["pulls"],
|
| 235 |
+
title="Pull Request List",
|
| 236 |
+
description="Summaries for GitHub pull requests.",
|
| 237 |
+
mime_type=HTML_MIME_TYPE,
|
| 238 |
+
)
|
| 239 |
+
def pull_requests_resource() -> str:
|
| 240 |
+
return """
|
| 241 |
+
<div id="pulls-list"></div>
|
| 242 |
+
<script>
|
| 243 |
+
let lastPulls;
|
| 244 |
+
const PATHS = [
|
| 245 |
+
["toolResult"],
|
| 246 |
+
["toolOutput","result"],
|
| 247 |
+
["toolOutput"],
|
| 248 |
+
["result"],
|
| 249 |
+
["payload","data"],
|
| 250 |
+
["payload"],
|
| 251 |
+
["globals","toolOutput","result"],
|
| 252 |
+
["globals","toolOutput"],
|
| 253 |
+
["globals","widget","props","result"],
|
| 254 |
+
];
|
| 255 |
+
function pick(data) {
|
| 256 |
+
if (!data || typeof data !== "object") return undefined;
|
| 257 |
+
for (const path of PATHS) {
|
| 258 |
+
let node = data;
|
| 259 |
+
for (const key of path) {
|
| 260 |
+
if (!node || typeof node !== "object") {
|
| 261 |
+
node = undefined;
|
| 262 |
+
break;
|
| 263 |
+
}
|
| 264 |
+
node = node[key];
|
| 265 |
+
}
|
| 266 |
+
if (node !== undefined) return node;
|
| 267 |
+
}
|
| 268 |
+
return undefined;
|
| 269 |
+
}
|
| 270 |
+
function handle(detail) {
|
| 271 |
+
const candidate = pick(detail) ?? pick(detail?.globals);
|
| 272 |
+
if (candidate !== undefined) {
|
| 273 |
+
if (Array.isArray(candidate)) {
|
| 274 |
+
lastPulls = candidate;
|
| 275 |
+
} else if (candidate) {
|
| 276 |
+
lastPulls = [candidate];
|
| 277 |
+
} else {
|
| 278 |
+
lastPulls = [];
|
| 279 |
+
}
|
| 280 |
+
return true;
|
| 281 |
+
}
|
| 282 |
+
return false;
|
| 283 |
+
}
|
| 284 |
+
function updateFromEvent(event) {
|
| 285 |
+
if (!event) return false;
|
| 286 |
+
return handle(event.detail || {});
|
| 287 |
+
}
|
| 288 |
+
function captureInitial() {
|
| 289 |
+
return handle(window.openai || {});
|
| 290 |
+
}
|
| 291 |
+
function currentPulls() {
|
| 292 |
+
if (lastPulls === undefined) {
|
| 293 |
+
if (!captureInitial()) {
|
| 294 |
+
lastPulls = [];
|
| 295 |
+
}
|
| 296 |
+
}
|
| 297 |
+
if (Array.isArray(lastPulls)) return lastPulls;
|
| 298 |
+
if (lastPulls && typeof lastPulls === "object") return [lastPulls];
|
| 299 |
+
return [];
|
| 300 |
+
}
|
| 301 |
+
function render() {
|
| 302 |
+
const pulls = currentPulls();
|
| 303 |
+
const root = document.getElementById("pulls-list");
|
| 304 |
+
if (!root) return;
|
| 305 |
+
if (!pulls.length) {
|
| 306 |
+
root.innerHTML = "<p>No pull requests returned.</p>";
|
| 307 |
+
return;
|
| 308 |
+
}
|
| 309 |
+
let html = "<h3>Pull Requests</h3><ul style='margin:0;padding-left:1.2rem;'>";
|
| 310 |
+
for (const pr of pulls) {
|
| 311 |
+
html += `
|
| 312 |
+
<li style="margin-bottom:0.5rem;">
|
| 313 |
+
<a href="${pr.html_url}" target="_blank">#${pr.number} — ${pr.title}</a>
|
| 314 |
+
<div style="font-size:12px;color:#475569;">${pr.state}${pr.draft ? " · draft" : ""} · ${pr.author || "unknown"}</div>
|
| 315 |
+
<div style="font-size:12px;color:#475569;">${pr.head || ""} ⇢ ${pr.base || ""}</div>
|
| 316 |
+
</li>
|
| 317 |
+
`;
|
| 318 |
+
}
|
| 319 |
+
html += "</ul>";
|
| 320 |
+
root.innerHTML = html;
|
| 321 |
+
}
|
| 322 |
+
render();
|
| 323 |
+
const eventNames = ["openai:tool_result","openai:set_globals","openai:tool_output","openai:result","openai:response"];
|
| 324 |
+
for (const name of eventNames) {
|
| 325 |
+
window.addEventListener(name, (event) => {
|
| 326 |
+
const changed = updateFromEvent(event);
|
| 327 |
+
if (changed) render();
|
| 328 |
+
});
|
| 329 |
+
}
|
| 330 |
+
</script>
|
| 331 |
+
"""
|
| 332 |
+
|
| 333 |
+
|
| 334 |
+
@server.resource(
|
| 335 |
+
RESOURCE_URIS["readme"],
|
| 336 |
+
title="README Viewer",
|
| 337 |
+
description="Render README content as preformatted text.",
|
| 338 |
+
mime_type=HTML_MIME_TYPE,
|
| 339 |
+
)
|
| 340 |
+
def readme_resource() -> str:
|
| 341 |
+
return """
|
| 342 |
+
<div id="readme-viewer"></div>
|
| 343 |
+
<script>
|
| 344 |
+
let lastReadme;
|
| 345 |
+
function pickFrom(detail) {
|
| 346 |
+
if (!detail || typeof detail !== "object") return undefined;
|
| 347 |
+
if (detail.toolResult) return detail.toolResult;
|
| 348 |
+
if (detail.toolOutput?.result) return detail.toolOutput.result;
|
| 349 |
+
if (detail.payload?.data) return detail.payload.data;
|
| 350 |
+
if (detail.result) return detail.result;
|
| 351 |
+
if (detail.toolOutput) return detail.toolOutput;
|
| 352 |
+
if (detail.payload) return detail.payload;
|
| 353 |
+
return undefined;
|
| 354 |
+
}
|
| 355 |
+
function updateFromEvent(event) {
|
| 356 |
+
if (!event) return false;
|
| 357 |
+
const detail = event.detail || {};
|
| 358 |
+
const candidate = pickFrom(detail) ?? pickFrom(detail.globals);
|
| 359 |
+
if (candidate !== undefined) {
|
| 360 |
+
lastReadme = candidate;
|
| 361 |
+
return true;
|
| 362 |
+
}
|
| 363 |
+
return false;
|
| 364 |
+
}
|
| 365 |
+
function currentReadme() {
|
| 366 |
+
if (lastReadme === undefined) {
|
| 367 |
+
lastReadme = {};
|
| 368 |
+
}
|
| 369 |
+
return lastReadme || {};
|
| 370 |
+
}
|
| 371 |
+
function render() {
|
| 372 |
+
const result = currentReadme();
|
| 373 |
+
const root = document.getElementById("readme-viewer");
|
| 374 |
+
if (!root) return;
|
| 375 |
+
const hasError = Boolean(result.error);
|
| 376 |
+
const text = result.content || (hasError ? "" : "README not available.");
|
| 377 |
+
const errorHtml = hasError
|
| 378 |
+
? `<div style="margin:0.5rem 0;padding:0.5rem;background:#fee2e2;border:1px solid #fecaca;border-radius:6px;color:#b91c1c;font-size:13px;">${result.error}</div>`
|
| 379 |
+
: "";
|
| 380 |
+
root.innerHTML = `
|
| 381 |
+
<h3>README (${result.path || "latest"})</h3>
|
| 382 |
+
${errorHtml}
|
| 383 |
+
<pre style="background:#0f172a0d;border:1px solid #cbd5f5;border-radius:6px;padding:0.75rem;white-space:pre-wrap;overflow:auto;max-height:360px;"></pre>
|
| 384 |
+
`;
|
| 385 |
+
const pre = root.querySelector("pre");
|
| 386 |
+
if (pre) {
|
| 387 |
+
pre.textContent = text;
|
| 388 |
+
}
|
| 389 |
+
}
|
| 390 |
+
render();
|
| 391 |
+
const eventNames = ["openai:tool_result","openai:set_globals","openai:tool_output","openai:result","openai:response"];
|
| 392 |
+
for (const name of eventNames) {
|
| 393 |
+
window.addEventListener(name, (event) => {
|
| 394 |
+
const changed = updateFromEvent(event);
|
| 395 |
+
if (changed) render();
|
| 396 |
+
});
|
| 397 |
+
}
|
| 398 |
+
</script>
|
| 399 |
+
"""
|
| 400 |
+
|
| 401 |
+
|
| 402 |
+
@server.resource(
|
| 403 |
+
RESOURCE_URIS["contributors"],
|
| 404 |
+
title="Contributor List",
|
| 405 |
+
description="Display contributors and their commit counts.",
|
| 406 |
+
mime_type=HTML_MIME_TYPE,
|
| 407 |
+
)
|
| 408 |
+
def contributors_resource() -> str:
|
| 409 |
+
return """
|
| 410 |
+
<div id="contributors-list"></div>
|
| 411 |
+
<script>
|
| 412 |
+
let lastContributors;
|
| 413 |
+
const PATHS = [
|
| 414 |
+
["toolResult"],
|
| 415 |
+
["toolOutput","result"],
|
| 416 |
+
["toolOutput"],
|
| 417 |
+
["result"],
|
| 418 |
+
["payload","data"],
|
| 419 |
+
["payload"],
|
| 420 |
+
["globals","toolOutput","result"],
|
| 421 |
+
["globals","toolOutput"],
|
| 422 |
+
["globals","widget","props","result"],
|
| 423 |
+
];
|
| 424 |
+
function pick(data) {
|
| 425 |
+
if (!data || typeof data !== "object") return undefined;
|
| 426 |
+
for (const path of PATHS) {
|
| 427 |
+
let node = data;
|
| 428 |
+
for (const key of path) {
|
| 429 |
+
if (!node || typeof node !== "object") {
|
| 430 |
+
node = undefined;
|
| 431 |
+
break;
|
| 432 |
+
}
|
| 433 |
+
node = node[key];
|
| 434 |
+
}
|
| 435 |
+
if (node !== undefined) return node;
|
| 436 |
+
}
|
| 437 |
+
return undefined;
|
| 438 |
+
}
|
| 439 |
+
function handle(detail) {
|
| 440 |
+
const candidate = pick(detail) ?? pick(detail?.globals);
|
| 441 |
+
if (candidate !== undefined) {
|
| 442 |
+
if (Array.isArray(candidate)) {
|
| 443 |
+
lastContributors = candidate;
|
| 444 |
+
} else if (candidate) {
|
| 445 |
+
lastContributors = [candidate];
|
| 446 |
+
} else {
|
| 447 |
+
lastContributors = [];
|
| 448 |
+
}
|
| 449 |
+
return true;
|
| 450 |
+
}
|
| 451 |
+
return false;
|
| 452 |
+
}
|
| 453 |
+
function updateFromEvent(event) {
|
| 454 |
+
if (!event) return false;
|
| 455 |
+
return handle(event.detail || {});
|
| 456 |
+
}
|
| 457 |
+
function captureInitial() {
|
| 458 |
+
return handle(window.openai || {});
|
| 459 |
+
}
|
| 460 |
+
function currentContributors() {
|
| 461 |
+
if (lastContributors === undefined) {
|
| 462 |
+
if (!captureInitial()) {
|
| 463 |
+
lastContributors = [];
|
| 464 |
+
}
|
| 465 |
+
}
|
| 466 |
+
if (Array.isArray(lastContributors)) return lastContributors;
|
| 467 |
+
if (lastContributors && typeof lastContributors === "object") return [lastContributors];
|
| 468 |
+
return [];
|
| 469 |
+
}
|
| 470 |
+
function render() {
|
| 471 |
+
const people = Array.isArray(currentContributors()) ? currentContributors() : [];
|
| 472 |
+
const root = document.getElementById("contributors-list");
|
| 473 |
+
if (!root) return;
|
| 474 |
+
if (!people.length) {
|
| 475 |
+
root.innerHTML = "<p>No contributors returned.</p>";
|
| 476 |
+
return;
|
| 477 |
+
}
|
| 478 |
+
let html = "<h3>Top Contributors</h3><ul style='list-style:none;margin:0;padding:0;display:flex;flex-direction:column;gap:0.35rem;'>";
|
| 479 |
+
for (const person of people) {
|
| 480 |
+
html += `
|
| 481 |
+
<li style="display:flex;align-items:center;gap:0.5rem;">
|
| 482 |
+
<span style="font-weight:600;">${person.login || "unknown"}</span>
|
| 483 |
+
<span style="font-size:12px;color:#475569;">${person.contributions || 0} commits</span>
|
| 484 |
+
<a href="${person.html_url}" target="_blank" style="font-size:12px;">profile ↗</a>
|
| 485 |
+
</li>
|
| 486 |
+
`;
|
| 487 |
+
}
|
| 488 |
+
html += "</ul>";
|
| 489 |
+
root.innerHTML = html;
|
| 490 |
+
}
|
| 491 |
+
render();
|
| 492 |
+
const eventNames = ["openai:tool_result","openai:set_globals","openai:tool_output","openai:result","openai:response"];
|
| 493 |
+
for (const name of eventNames) {
|
| 494 |
+
window.addEventListener(name, (event) => {
|
| 495 |
+
const changed = updateFromEvent(event);
|
| 496 |
+
if (changed) render();
|
| 497 |
+
});
|
| 498 |
+
}
|
| 499 |
+
</script>
|
| 500 |
+
"""
|
| 501 |
+
|
| 502 |
+
|
| 503 |
+
@server.resource(
|
| 504 |
+
RESOURCE_URIS["files"],
|
| 505 |
+
title="Repository Files",
|
| 506 |
+
description="Visualize the returned Git tree entries.",
|
| 507 |
+
mime_type=HTML_MIME_TYPE,
|
| 508 |
+
)
|
| 509 |
+
def files_resource() -> str:
|
| 510 |
+
return """
|
| 511 |
+
<div id="files-list"></div>
|
| 512 |
+
<script>
|
| 513 |
+
let lastFiles;
|
| 514 |
+
const PATHS = [
|
| 515 |
+
["toolResult"],
|
| 516 |
+
["toolOutput","result"],
|
| 517 |
+
["toolOutput"],
|
| 518 |
+
["result"],
|
| 519 |
+
["payload","data"],
|
| 520 |
+
["payload"],
|
| 521 |
+
["globals","toolOutput","result"],
|
| 522 |
+
["globals","toolOutput"],
|
| 523 |
+
["globals","widget","props","result"],
|
| 524 |
+
];
|
| 525 |
+
function pick(data) {
|
| 526 |
+
if (!data || typeof data !== "object") return undefined;
|
| 527 |
+
for (const path of PATHS) {
|
| 528 |
+
let node = data;
|
| 529 |
+
for (const key of path) {
|
| 530 |
+
if (!node || typeof node !== "object") {
|
| 531 |
+
node = undefined;
|
| 532 |
+
break;
|
| 533 |
+
}
|
| 534 |
+
node = node[key];
|
| 535 |
+
}
|
| 536 |
+
if (node !== undefined) return node;
|
| 537 |
+
}
|
| 538 |
+
return undefined;
|
| 539 |
+
}
|
| 540 |
+
function handle(detail) {
|
| 541 |
+
const candidate = pick(detail) ?? pick(detail?.globals);
|
| 542 |
+
if (candidate !== undefined) {
|
| 543 |
+
if (Array.isArray(candidate)) {
|
| 544 |
+
lastFiles = candidate;
|
| 545 |
+
} else if (candidate) {
|
| 546 |
+
lastFiles = [candidate];
|
| 547 |
+
} else {
|
| 548 |
+
lastFiles = [];
|
| 549 |
+
}
|
| 550 |
+
return true;
|
| 551 |
+
}
|
| 552 |
+
return false;
|
| 553 |
+
}
|
| 554 |
+
function updateFromEvent(event) {
|
| 555 |
+
if (!event) return false;
|
| 556 |
+
return handle(event.detail || {});
|
| 557 |
+
}
|
| 558 |
+
function captureInitial() {
|
| 559 |
+
return handle(window.openai || {});
|
| 560 |
+
}
|
| 561 |
+
function currentFiles() {
|
| 562 |
+
if (lastFiles === undefined) {
|
| 563 |
+
if (!captureInitial()) {
|
| 564 |
+
lastFiles = [];
|
| 565 |
+
}
|
| 566 |
+
}
|
| 567 |
+
if (Array.isArray(lastFiles)) return lastFiles;
|
| 568 |
+
if (lastFiles && typeof lastFiles === "object") return [lastFiles];
|
| 569 |
+
return [];
|
| 570 |
+
}
|
| 571 |
+
function render() {
|
| 572 |
+
const nodes = Array.isArray(currentFiles()) ? currentFiles() : [];
|
| 573 |
+
const root = document.getElementById("files-list");
|
| 574 |
+
if (!root) return;
|
| 575 |
+
if (!nodes.length) {
|
| 576 |
+
root.innerHTML = "<p>No files returned.</p>";
|
| 577 |
+
return;
|
| 578 |
+
}
|
| 579 |
+
let html = "<h3>Files</h3><ul style='margin:0;padding-left:1.2rem;'>";
|
| 580 |
+
for (const node of nodes) {
|
| 581 |
+
const icon = node.type === "tree" ? "📁" : "📄";
|
| 582 |
+
const sizeInfo = node.size ? ` <span style='color:#475569;font-size:12px;'>${node.size} bytes</span>` : "";
|
| 583 |
+
const link = node.html_url ? `<a href='${node.html_url}' target='_blank'>${node.path}</a>` : node.path;
|
| 584 |
+
html += `<li style="margin-bottom:0.35rem;">${icon} ${link}${sizeInfo}</li>`;
|
| 585 |
+
}
|
| 586 |
+
html += "</ul>";
|
| 587 |
+
root.innerHTML = html;
|
| 588 |
+
}
|
| 589 |
+
render();
|
| 590 |
+
const eventNames = ["openai:tool_result","openai:set_globals","openai:tool_output","openai:result","openai:response"];
|
| 591 |
+
for (const name of eventNames) {
|
| 592 |
+
window.addEventListener(name, (event) => {
|
| 593 |
+
const changed = updateFromEvent(event);
|
| 594 |
+
if (changed) render();
|
| 595 |
+
});
|
| 596 |
+
}
|
| 597 |
+
</script>
|
| 598 |
+
"""
|
| 599 |
+
|
| 600 |
+
|
| 601 |
+
@server.resource(
|
| 602 |
+
RESOURCE_URIS["search"],
|
| 603 |
+
title="Repository Search Results",
|
| 604 |
+
description="Render search results with metadata.",
|
| 605 |
+
mime_type=HTML_MIME_TYPE,
|
| 606 |
+
)
|
| 607 |
+
def search_resource() -> str:
|
| 608 |
+
return """
|
| 609 |
+
<div id="search-results"></div>
|
| 610 |
+
<script>
|
| 611 |
+
let lastSearch;
|
| 612 |
+
const PATHS = [
|
| 613 |
+
["toolResult"],
|
| 614 |
+
["toolOutput","result"],
|
| 615 |
+
["toolOutput"],
|
| 616 |
+
["result"],
|
| 617 |
+
["payload","data"],
|
| 618 |
+
["payload"],
|
| 619 |
+
["globals","toolOutput","result"],
|
| 620 |
+
["globals","toolOutput"],
|
| 621 |
+
["globals","widget","props","result"],
|
| 622 |
+
];
|
| 623 |
+
function pick(data) {
|
| 624 |
+
if (!data || typeof data !== "object") return undefined;
|
| 625 |
+
for (const path of PATHS) {
|
| 626 |
+
let node = data;
|
| 627 |
+
for (const key of path) {
|
| 628 |
+
if (!node || typeof node !== "object") {
|
| 629 |
+
node = undefined;
|
| 630 |
+
break;
|
| 631 |
+
}
|
| 632 |
+
node = node[key];
|
| 633 |
+
}
|
| 634 |
+
if (node !== undefined) return node;
|
| 635 |
+
}
|
| 636 |
+
return undefined;
|
| 637 |
+
}
|
| 638 |
+
function handle(detail) {
|
| 639 |
+
const candidate = pick(detail) ?? pick(detail?.globals);
|
| 640 |
+
if (candidate !== undefined) {
|
| 641 |
+
if (Array.isArray(candidate)) {
|
| 642 |
+
lastSearch = candidate;
|
| 643 |
+
} else if (candidate) {
|
| 644 |
+
lastSearch = [candidate];
|
| 645 |
+
} else {
|
| 646 |
+
lastSearch = [];
|
| 647 |
+
}
|
| 648 |
+
return true;
|
| 649 |
+
}
|
| 650 |
+
return false;
|
| 651 |
+
}
|
| 652 |
+
function updateFromEvent(event) {
|
| 653 |
+
if (!event) return false;
|
| 654 |
+
return handle(event.detail || {});
|
| 655 |
+
}
|
| 656 |
+
function captureInitial() {
|
| 657 |
+
return handle(window.openai || {});
|
| 658 |
+
}
|
| 659 |
+
function currentSearch() {
|
| 660 |
+
if (lastSearch === undefined) {
|
| 661 |
+
if (!captureInitial()) {
|
| 662 |
+
lastSearch = [];
|
| 663 |
+
}
|
| 664 |
+
}
|
| 665 |
+
if (Array.isArray(lastSearch)) return lastSearch;
|
| 666 |
+
if (lastSearch && typeof lastSearch === "object") return [lastSearch];
|
| 667 |
+
return [];
|
| 668 |
+
}
|
| 669 |
+
function render() {
|
| 670 |
+
const repos = Array.isArray(currentSearch()) ? currentSearch() : [];
|
| 671 |
+
const root = document.getElementById("search-results");
|
| 672 |
+
if (!root) return;
|
| 673 |
+
if (!repos.length) {
|
| 674 |
+
root.innerHTML = "<p>No repositories returned for this query.</p>";
|
| 675 |
+
return;
|
| 676 |
+
}
|
| 677 |
+
let html = "<h3>Search Results</h3><ul style='margin:0;padding-left:1.2rem;'>";
|
| 678 |
+
for (const repo of repos) {
|
| 679 |
+
html += `
|
| 680 |
+
<li style="margin-bottom:0.6rem;">
|
| 681 |
+
<a href="${repo.html_url}" target="_blank">${repo.full_name}</a>
|
| 682 |
+
<div style="font-size:12px;color:#475569;">⭐ ${repo.stargazers_count || 0} · Updated ${repo.updated_at || "recently"}</div>
|
| 683 |
+
<div style="font-size:13px;color:#1f2933;margin-top:0.2rem;">${repo.description || ""}</div>
|
| 684 |
+
</li>
|
| 685 |
+
`;
|
| 686 |
+
}
|
| 687 |
+
html += "</ul>";
|
| 688 |
+
root.innerHTML = html;
|
| 689 |
+
}
|
| 690 |
+
render();
|
| 691 |
+
const eventNames = ["openai:tool_result","openai:set_globals","openai:tool_output","openai:result","openai:response"];
|
| 692 |
+
for (const name of eventNames) {
|
| 693 |
+
window.addEventListener(name, (event) => {
|
| 694 |
+
const changed = updateFromEvent(event);
|
| 695 |
+
if (changed) render();
|
| 696 |
+
});
|
| 697 |
+
}
|
| 698 |
+
</script>
|
| 699 |
+
"""
|
| 700 |
+
|
| 701 |
+
|
| 702 |
+
def _summarize_repo(data: Dict[str, Any]) -> Dict[str, Any]:
|
| 703 |
+
return {
|
| 704 |
+
"id": data.get("id"),
|
| 705 |
+
"full_name": data.get("full_name"),
|
| 706 |
+
"description": data.get("description"),
|
| 707 |
+
"visibility": data.get("visibility"),
|
| 708 |
+
"default_branch": data.get("default_branch"),
|
| 709 |
+
"language": data.get("language"),
|
| 710 |
+
"license": (data.get("license") or {}).get("spdx_id") if data.get("license") else None,
|
| 711 |
+
"stargazers_count": data.get("stargazers_count"),
|
| 712 |
+
"forks_count": data.get("forks_count"),
|
| 713 |
+
"open_issues_count": data.get("open_issues_count"),
|
| 714 |
+
"html_url": data.get("html_url"),
|
| 715 |
+
"topics": data.get("topics", []),
|
| 716 |
+
"updated_at": data.get("updated_at"),
|
| 717 |
+
}
|
| 718 |
+
|
| 719 |
+
|
| 720 |
+
def _summarize_issue(issue: Dict[str, Any]) -> Dict[str, Any]:
|
| 721 |
+
return {
|
| 722 |
+
"number": issue.get("number"),
|
| 723 |
+
"title": issue.get("title"),
|
| 724 |
+
"state": issue.get("state"),
|
| 725 |
+
"html_url": issue.get("html_url"),
|
| 726 |
+
"author": (issue.get("user") or {}).get("login"),
|
| 727 |
+
"comments": issue.get("comments"),
|
| 728 |
+
"labels": [label.get("name") for label in issue.get("labels", [])],
|
| 729 |
+
"created_at": issue.get("created_at"),
|
| 730 |
+
"updated_at": issue.get("updated_at"),
|
| 731 |
+
"assignees": [assignee.get("login") for assignee in issue.get("assignees", [])],
|
| 732 |
+
}
|
| 733 |
+
|
| 734 |
+
|
| 735 |
+
def _summarize_pr(pr: Dict[str, Any]) -> Dict[str, Any]:
|
| 736 |
+
return {
|
| 737 |
+
"number": pr.get("number"),
|
| 738 |
+
"title": pr.get("title"),
|
| 739 |
+
"state": pr.get("state"),
|
| 740 |
+
"draft": pr.get("draft"),
|
| 741 |
+
"html_url": pr.get("html_url"),
|
| 742 |
+
"author": (pr.get("user") or {}).get("login"),
|
| 743 |
+
"created_at": pr.get("created_at"),
|
| 744 |
+
"updated_at": pr.get("updated_at"),
|
| 745 |
+
"merged_at": pr.get("merged_at"),
|
| 746 |
+
"head": (pr.get("head") or {}).get("ref"),
|
| 747 |
+
"base": (pr.get("base") or {}).get("ref"),
|
| 748 |
+
}
|
| 749 |
+
|
| 750 |
+
|
| 751 |
+
def _summarize_contributor(contributor: Dict[str, Any]) -> Dict[str, Any]:
|
| 752 |
+
return {
|
| 753 |
+
"login": contributor.get("login"),
|
| 754 |
+
"contributions": contributor.get("contributions"),
|
| 755 |
+
"html_url": contributor.get("html_url"),
|
| 756 |
+
"type": contributor.get("type"),
|
| 757 |
+
}
|
| 758 |
+
|
| 759 |
+
|
| 760 |
+
def _handle_errors(coro):
|
| 761 |
+
async def wrapper(*args, **kwargs):
|
| 762 |
+
try:
|
| 763 |
+
return await coro(*args, **kwargs)
|
| 764 |
+
except GitHubAPIError as exc:
|
| 765 |
+
return {"error": str(exc)}
|
| 766 |
+
|
| 767 |
+
return wrapper
|
| 768 |
+
|
| 769 |
+
|
| 770 |
+
@server.tool(meta=_tool_meta(RESOURCE_URIS["repo_info"]))
|
| 771 |
+
async def get_repo_info(owner: str, repo: str) -> Dict[str, Any]:
|
| 772 |
+
"""Return metadata for a repository."""
|
| 773 |
+
data = await _client.get_repo_info(owner, repo)
|
| 774 |
+
return _summarize_repo(data)
|
| 775 |
+
|
| 776 |
+
|
| 777 |
+
@server.tool(meta=_tool_meta(RESOURCE_URIS["issues"]))
|
| 778 |
+
async def list_issues(
|
| 779 |
+
owner: str,
|
| 780 |
+
repo: str,
|
| 781 |
+
state: str = "open",
|
| 782 |
+
labels: Optional[List[str]] = None,
|
| 783 |
+
limit: int = 25,
|
| 784 |
+
include_pull_requests: bool = False,
|
| 785 |
+
) -> List[Dict[str, Any]]:
|
| 786 |
+
"""List issues in a repository."""
|
| 787 |
+
issues = await _client.list_issues(owner, repo, state, labels, limit, include_pull_requests)
|
| 788 |
+
return [_summarize_issue(issue) for issue in issues]
|
| 789 |
+
|
| 790 |
+
|
| 791 |
+
@server.tool(meta=_tool_meta(RESOURCE_URIS["pulls"]))
|
| 792 |
+
async def list_pull_requests(owner: str, repo: str, state: str = "open", limit: int = 25) -> List[Dict[str, Any]]:
|
| 793 |
+
"""List pull requests for a repository."""
|
| 794 |
+
pulls = await _client.list_pull_requests(owner, repo, state, limit)
|
| 795 |
+
return [_summarize_pr(pr) for pr in pulls]
|
| 796 |
+
|
| 797 |
+
|
| 798 |
+
@server.tool(meta=_tool_meta(RESOURCE_URIS["readme"]))
|
| 799 |
+
async def get_readme(owner: str, repo: str, ref: Optional[str] = None) -> Dict[str, Any]:
|
| 800 |
+
"""Fetch the README contents for a repository."""
|
| 801 |
+
return await _client.get_readme(owner, repo, ref)
|
| 802 |
+
|
| 803 |
+
|
| 804 |
+
@server.tool(meta=_tool_meta(RESOURCE_URIS["contributors"]))
|
| 805 |
+
async def list_contributors(owner: str, repo: str, limit: int = 25, include_anonymous: bool = False) -> List[Dict[str, Any]]:
|
| 806 |
+
"""Return contributors for a repository."""
|
| 807 |
+
contributors = await _client.list_contributors(owner, repo, limit, include_anonymous)
|
| 808 |
+
return [_summarize_contributor(contributor) for contributor in contributors]
|
| 809 |
+
|
| 810 |
+
|
| 811 |
+
@server.tool(meta=_tool_meta(RESOURCE_URIS["files"]))
|
| 812 |
+
async def list_files(
|
| 813 |
+
owner: str,
|
| 814 |
+
repo: str,
|
| 815 |
+
ref: str = "main",
|
| 816 |
+
directory: Optional[str] = None,
|
| 817 |
+
limit: int = 200,
|
| 818 |
+
) -> List[Dict[str, Any]]:
|
| 819 |
+
"""List files (tree entries) for a branch or tag."""
|
| 820 |
+
entries = await _client.list_files(owner, repo, ref, directory, limit)
|
| 821 |
+
base_url = f"https://github.com/{owner}/{repo}"
|
| 822 |
+
enriched: List[Dict[str, Any]] = []
|
| 823 |
+
for entry in entries:
|
| 824 |
+
path = entry.get("path")
|
| 825 |
+
if not path:
|
| 826 |
+
enriched.append(entry)
|
| 827 |
+
continue
|
| 828 |
+
entry_type = entry.get("type")
|
| 829 |
+
html_url = f"{base_url}/tree/{ref}/{path}" if entry_type == "tree" else f"{base_url}/blob/{ref}/{path}"
|
| 830 |
+
enriched.append({**entry, "html_url": html_url})
|
| 831 |
+
return enriched
|
| 832 |
+
|
| 833 |
+
|
| 834 |
+
@server.tool(meta=_tool_meta(RESOURCE_URIS["search"]))
|
| 835 |
+
async def search_repos(query: str, sort: Optional[str] = None, order: str = "desc", limit: int = 25) -> List[Dict[str, Any]]:
|
| 836 |
+
"""Search repositories using the GitHub search API."""
|
| 837 |
+
data = await _client.search_repositories(query, sort, order, limit)
|
| 838 |
+
return [_summarize_repo(repo) for repo in data]
|
| 839 |
+
|
| 840 |
+
|
| 841 |
+
if __name__ == "__main__":
|
| 842 |
+
server.run(transport="streamable-http")
|