@cloudgrid-io/mcp 0.4.6 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,176 @@
1
+ // BM25 keyword search over the CloudGrid documentation corpus.
2
+ //
3
+ // Chunks markdown files by heading, builds an inverted index, and ranks by
4
+ // BM25. The DocsSearch class exposes a clean .search(query) → results interface
5
+ // so the backend can be swapped to semantic/embedding search later without
6
+ // changing the tool contract.
7
+
8
+ import { readFileSync, readdirSync } from "node:fs";
9
+
10
+ const STOPWORDS = new Set([
11
+ "a", "an", "the", "and", "or", "but", "in", "on", "at", "to", "for",
12
+ "of", "with", "by", "from", "is", "it", "as", "be", "was", "are",
13
+ "this", "that", "not", "if", "do", "so", "no", "can", "will", "all",
14
+ "has", "had", "have", "each", "which", "their", "you", "your", "its",
15
+ "any", "more", "also", "than", "then", "what", "when", "how", "who",
16
+ "up", "out", "about", "into", "over", "just", "some", "only",
17
+ ]);
18
+
19
+ function tokenize(text) {
20
+ return text
21
+ .toLowerCase()
22
+ .replace(/[^a-z0-9]+/g, " ")
23
+ .split(/\s+/)
24
+ .filter((t) => t.length > 1 && !STOPWORDS.has(t));
25
+ }
26
+
27
+ function termFrequency(tokens) {
28
+ const tf = Object.create(null);
29
+ for (const t of tokens) tf[t] = (tf[t] || 0) + 1;
30
+ return tf;
31
+ }
32
+
33
+ /** Trim a markdown body to a readable snippet. */
34
+ function makeSnippet(body, maxLen = 300) {
35
+ const clean = body
36
+ .replace(/```[\s\S]*?```/g, " ")
37
+ .replace(/\n+/g, " ")
38
+ .replace(/\s+/g, " ")
39
+ .trim();
40
+ return clean.length <= maxLen ? clean : clean.slice(0, maxLen - 1) + "\u2026";
41
+ }
42
+
43
+ /** Split a markdown document into chunks by headings (# or ##). */
44
+ function chunkMarkdown(source, content) {
45
+ const lines = content.split("\n");
46
+ const chunks = [];
47
+ let title = source;
48
+ let body = [];
49
+ let inFrontmatter = false;
50
+ let seenFirstLine = false;
51
+
52
+ for (const line of lines) {
53
+ // Skip YAML frontmatter (--- delimited block at the top).
54
+ if (line.trim() === "---" && !seenFirstLine) {
55
+ inFrontmatter = true;
56
+ seenFirstLine = true;
57
+ continue;
58
+ }
59
+ if (inFrontmatter) {
60
+ if (line.trim() === "---") inFrontmatter = false;
61
+ continue;
62
+ }
63
+ seenFirstLine = true;
64
+
65
+ const heading = line.match(/^(#{1,3})\s+(.+)/);
66
+ if (heading) {
67
+ if (body.length > 0) {
68
+ const text = body.join("\n").trim();
69
+ if (text) chunks.push({ title, body: text, source });
70
+ }
71
+ body = [];
72
+ title = heading[2].trim();
73
+ } else {
74
+ body.push(line);
75
+ }
76
+ }
77
+ const text = body.join("\n").trim();
78
+ if (text) chunks.push({ title, body: text, source });
79
+
80
+ return chunks;
81
+ }
82
+
83
+ export class DocsSearch {
84
+ constructor() {
85
+ /** @type {Array<{title: string, body: string, source: string, tokens: string[], tf: Record<string,number>}>} */
86
+ this.docs = [];
87
+ this.df = Object.create(null);
88
+ this.avgdl = 0;
89
+ this._built = false;
90
+ }
91
+
92
+ get size() {
93
+ return this.docs.length;
94
+ }
95
+
96
+ /** Add a single document chunk to the index. */
97
+ addDocument(title, body, source) {
98
+ // Weight the title 2x by repeating it in the token stream.
99
+ const tokens = tokenize(title + " " + title + " " + body);
100
+ this.docs.push({ title, body, source, tokens, tf: termFrequency(tokens) });
101
+ }
102
+
103
+ /** Compute document frequencies and average document length. Call after all adds. */
104
+ build() {
105
+ const N = this.docs.length;
106
+ if (N === 0) return;
107
+ this.avgdl = this.docs.reduce((sum, d) => sum + d.tokens.length, 0) / N;
108
+ this.df = Object.create(null);
109
+ for (const doc of this.docs) {
110
+ const seen = new Set(doc.tokens);
111
+ for (const t of seen) this.df[t] = (this.df[t] || 0) + 1;
112
+ }
113
+ this._built = true;
114
+ }
115
+
116
+ /**
117
+ * Search the corpus. Returns the top-K results ranked by BM25.
118
+ * @param {string} query
119
+ * @param {number} topK
120
+ * @returns {Array<{title: string, snippet: string, source: string, score: number}>}
121
+ */
122
+ search(query, topK = 5) {
123
+ if (!this._built) this.build();
124
+ const qTokens = tokenize(query);
125
+ if (qTokens.length === 0) return [];
126
+
127
+ const N = this.docs.length;
128
+ const k1 = 1.5;
129
+ const b = 0.75;
130
+
131
+ const scored = this.docs.map((doc) => {
132
+ let score = 0;
133
+ for (const qt of qTokens) {
134
+ const tf = doc.tf[qt] || 0;
135
+ const df = this.df[qt] || 0;
136
+ if (df === 0 || tf === 0) continue;
137
+ const idf = Math.log((N - df + 0.5) / (df + 0.5) + 1);
138
+ score +=
139
+ idf * ((tf * (k1 + 1)) / (tf + k1 * (1 - b + (b * doc.tokens.length) / this.avgdl)));
140
+ }
141
+ return { doc, score };
142
+ });
143
+
144
+ return scored
145
+ .filter((s) => s.score > 0)
146
+ .sort((a, b) => b.score - a.score)
147
+ .slice(0, topK)
148
+ .map(({ doc, score }) => ({
149
+ title: doc.title,
150
+ snippet: makeSnippet(doc.body),
151
+ source: doc.source,
152
+ score: Math.round(score * 100) / 100,
153
+ }));
154
+ }
155
+ }
156
+
157
+ /**
158
+ * Load all .md files from src/corpus/, chunk by heading, and build the index.
159
+ * @param {DocsSearch} searchIndex
160
+ * @returns {DocsSearch}
161
+ */
162
+ export function loadCorpus(searchIndex) {
163
+ const corpusDir = new URL("./corpus/", import.meta.url);
164
+ const files = readdirSync(corpusDir).filter((f) => f.endsWith(".md")).sort();
165
+
166
+ for (const file of files) {
167
+ const content = readFileSync(new URL(file, corpusDir), "utf-8");
168
+ const source = file.replace(/\.md$/, "");
169
+ for (const chunk of chunkMarkdown(source, content)) {
170
+ searchIndex.addDocument(chunk.title, chunk.body, chunk.source);
171
+ }
172
+ }
173
+
174
+ searchIndex.build();
175
+ return searchIndex;
176
+ }
package/src/docs.js ADDED
@@ -0,0 +1,135 @@
1
+ // CloudGrid MCP server — docs edition (public, read-only, no auth).
2
+ //
3
+ // A public documentation search server exposing search_cloudgrid_documentation
4
+ // (and an optional cloudgrid_quickstart_guide) over MCP Streamable HTTP. The
5
+ // same Express + StreamableHTTP pattern as the web edition (src/web.js),
6
+ // stripped to the minimum: no auth, no OAuth, no session identity, no secrets.
7
+ // Safe to expose anonymously.
8
+ //
9
+ // The corpus (src/corpus/*.md) is indexed at startup with BM25 keyword search.
10
+ // The search backend is behind a clean interface so it can be swapped to
11
+ // semantic/embedding search later without changing the tool contract.
12
+ //
13
+ // Run: PORT=8080 node src/docs.js Health: GET /healthz
14
+
15
+ import { readFileSync } from "node:fs";
16
+ import { randomUUID } from "node:crypto";
17
+ import express from "express";
18
+ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
19
+ import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
20
+ import { isInitializeRequest } from "@modelcontextprotocol/sdk/types.js";
21
+ import { z } from "zod";
22
+ import { DocsSearch, loadCorpus } from "./docs-search.js";
23
+
24
+ const { version } = JSON.parse(readFileSync(new URL("../package.json", import.meta.url)));
25
+
26
+ const PORT = Number(process.env.PORT || 8080);
27
+
28
+ // Build the search index once at startup.
29
+ const search = loadCorpus(new DocsSearch());
30
+
31
+ // Load the quickstart guide verbatim.
32
+ const QUICKSTART = readFileSync(new URL("./corpus/cookbook.md", import.meta.url), "utf-8");
33
+
34
+ // ── Tool helpers (same convention as tools.js) ────────────────────────────────
35
+ function ok(text) {
36
+ return { content: [{ type: "text", text }] };
37
+ }
38
+
39
+ function createDocsServer() {
40
+ const server = new McpServer({ name: "cloudgrid-docs", version });
41
+
42
+ server.tool(
43
+ "search_cloudgrid_documentation",
44
+ "Search CloudGrid documentation — guides, tutorials, CLI reference, MCP setup, and skill descriptions. Returns the most relevant doc chunks with title, snippet, and source path.",
45
+ {
46
+ query: z
47
+ .string()
48
+ .describe(
49
+ "The search query, e.g. 'deploy a database app' or 'add the MCP to Cursor'",
50
+ ),
51
+ },
52
+ async ({ query }) => {
53
+ const results = search.search(query, 5);
54
+ if (results.length === 0) {
55
+ return ok("No documentation matched your query. Try different keywords.");
56
+ }
57
+ const text = results
58
+ .map(
59
+ (r, i) => `### ${i + 1}. ${r.title}\n${r.snippet}\n_Source: ${r.source}_`,
60
+ )
61
+ .join("\n\n---\n\n");
62
+ return ok(text);
63
+ },
64
+ );
65
+
66
+ server.tool(
67
+ "cloudgrid_quickstart_guide",
68
+ "Get the CloudGrid quickstart guide — the canonical build-and-ship loop from scaffold to deploy to feedback.",
69
+ {},
70
+ async () => ok(QUICKSTART),
71
+ );
72
+
73
+ return server;
74
+ }
75
+
76
+ // ── HTTP server (mirrors src/web.js, no auth) ─────────────────────────────────
77
+ const app = express();
78
+ app.use(express.json());
79
+
80
+ app.get("/healthz", (_req, res) => res.json({ ok: true, edition: "docs" }));
81
+
82
+ const transports = Object.create(null);
83
+
84
+ app.post("/mcp", async (req, res) => {
85
+ const sessionId = req.headers["mcp-session-id"];
86
+ let transport = sessionId ? transports[sessionId] : undefined;
87
+
88
+ if (transport) {
89
+ await transport.handleRequest(req, res, req.body);
90
+ return;
91
+ }
92
+
93
+ if (sessionId || !isInitializeRequest(req.body)) {
94
+ res.status(400).json({
95
+ jsonrpc: "2.0",
96
+ error: { code: -32000, message: "No valid session. Send an initialize request first." },
97
+ id: null,
98
+ });
99
+ return;
100
+ }
101
+
102
+ const newSessionId = randomUUID();
103
+ transport = new StreamableHTTPServerTransport({
104
+ sessionIdGenerator: () => newSessionId,
105
+ onsessioninitialized: (sid) => {
106
+ transports[sid] = transport;
107
+ },
108
+ });
109
+ transport.onclose = () => {
110
+ if (transport.sessionId) delete transports[transport.sessionId];
111
+ };
112
+ const server = createDocsServer();
113
+ await server.connect(transport);
114
+ await transport.handleRequest(req, res, req.body);
115
+ });
116
+
117
+ async function handleSessionRequest(req, res) {
118
+ const sessionId = req.headers["mcp-session-id"];
119
+ const transport = sessionId ? transports[sessionId] : undefined;
120
+ if (!transport) {
121
+ res.status(400).send("Invalid or missing session id");
122
+ return;
123
+ }
124
+ await transport.handleRequest(req, res);
125
+ }
126
+
127
+ app.get("/mcp", handleSessionRequest);
128
+ app.delete("/mcp", handleSessionRequest);
129
+
130
+ app.listen(PORT, () => {
131
+ // eslint-disable-next-line no-console
132
+ console.error(
133
+ `cloudgrid-docs edition listening on :${PORT} (POST /mcp, GET /healthz) — ${search.size} chunks indexed`,
134
+ );
135
+ });
package/src/tools.js CHANGED
@@ -90,6 +90,9 @@ function resolveBundledCli() {
90
90
  }
91
91
  }
92
92
 
93
+ // Pin the CLI version for the lazy npx fallback so behaviour is reproducible.
94
+ const CLI_NPX_PKG = "@cloudgrid-io/cli@^0.9.20";
95
+
93
96
  async function runCloudgrid(args, opts = {}) {
94
97
  const cwd = resolveCwd(opts.cwd);
95
98
  const execOpts = {
@@ -99,7 +102,15 @@ async function runCloudgrid(args, opts = {}) {
99
102
  ...(cwd ? { cwd } : {}),
100
103
  };
101
104
 
102
- // 1. Try the bundled CLI (node <cliEntry> ...args)
105
+ const extract = (err) => {
106
+ const detail = [err && err.stdout, err && err.stderr, err && err.message]
107
+ .filter(Boolean)
108
+ .join("\n")
109
+ .trim();
110
+ return new Error(detail || "cloudgrid command failed");
111
+ };
112
+
113
+ // 1. Bundled CLI (the .mcpb ships it in its node_modules)
103
114
  const bundled = resolveBundledCli();
104
115
  if (bundled) {
105
116
  try {
@@ -110,29 +121,32 @@ async function runCloudgrid(args, opts = {}) {
110
121
  );
111
122
  return (stdout || stderr || "").trim() || "Done.";
112
123
  } catch (err) {
113
- const detail = [err && err.stdout, err && err.stderr, err && err.message]
114
- .filter(Boolean)
115
- .join("\n")
116
- .trim();
117
- throw new Error(detail || "cloudgrid command failed");
124
+ throw extract(err);
118
125
  }
119
126
  }
120
127
 
121
- // 2. Fallback: global `cloudgrid` on PATH
128
+ // 2. Global `cloudgrid` on PATH
122
129
  try {
123
130
  const { stdout, stderr } = await execFileAsync("cloudgrid", args, execOpts);
124
131
  return (stdout || stderr || "").trim() || "Done.";
125
132
  } catch (err) {
126
- if (err && err.code === "ENOENT") {
127
- throw new Error(
128
- "The cloudgrid CLI is not installed. Install it with: npm install -g @cloudgrid-io/cli",
129
- );
130
- }
131
- const detail = [err && err.stdout, err && err.stderr, err && err.message]
132
- .filter(Boolean)
133
- .join("\n")
134
- .trim();
135
- throw new Error(detail || "cloudgrid command failed");
133
+ if (!(err && err.code === "ENOENT")) throw extract(err);
134
+ // Not found — fall through to lazy npx fetch
135
+ }
136
+
137
+ // 3. Lazy fetch: npx -y @cloudgrid-io/cli@<pinned> <args>
138
+ // One-time download; npx caches it for subsequent invocations.
139
+ console.error("cloudgrid-mcp: fetching the CloudGrid CLI (first use)…");
140
+ const npx = process.platform === "win32" ? "npx.cmd" : "npx";
141
+ try {
142
+ const { stdout, stderr } = await execFileAsync(
143
+ npx,
144
+ ["-y", CLI_NPX_PKG, ...args],
145
+ execOpts,
146
+ );
147
+ return (stdout || stderr || "").trim() || "Done.";
148
+ } catch (err) {
149
+ throw extract(err);
136
150
  }
137
151
  }
138
152