@archbee/mcp 2.7.0 → 3.0.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.
- package/README.md +42 -229
- package/dist/index.js +180 -217
- package/dist/index.js.map +1 -1
- package/package.json +4 -5
- package/src/index.ts +221 -473
- package/dist/tool-schemas.js +0 -325
- package/dist/tool-schemas.js.map +0 -1
- package/src/tool-schemas.ts +0 -416
package/src/index.ts
CHANGED
|
@@ -1,539 +1,287 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
StreamableHTTPClientTransport,
|
|
5
|
+
StreamableHTTPError,
|
|
6
|
+
} from '@modelcontextprotocol/sdk/client/streamableHttp.js'
|
|
4
7
|
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
|
|
5
|
-
import {
|
|
6
|
-
import axios from 'axios'
|
|
8
|
+
import type { FetchLike } from '@modelcontextprotocol/sdk/shared/transport.js'
|
|
7
9
|
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
deleteContentSnippetSchema,
|
|
18
|
-
deleteDocSchema,
|
|
19
|
-
deleteDocSpaceSchema,
|
|
20
|
-
deleteSpaceGroupSchema,
|
|
21
|
-
deleteTemplateSchema,
|
|
22
|
-
deleteVariableSchema,
|
|
23
|
-
getAllDocsSchema,
|
|
24
|
-
getDocSchema,
|
|
25
|
-
listContentSnippetsSchema,
|
|
26
|
-
listSpaceGroupsSchema,
|
|
27
|
-
listTemplatesSchema,
|
|
28
|
-
listVariablesSchema,
|
|
29
|
-
searchQuerySchema,
|
|
30
|
-
searchTitleSchema,
|
|
31
|
-
updateContentSnippetSchema,
|
|
32
|
-
updateDocSchema,
|
|
33
|
-
updateDocSpaceSchema,
|
|
34
|
-
updateVariableSchema,
|
|
35
|
-
whoamiSchema,
|
|
36
|
-
} from './tool-schemas'
|
|
37
|
-
|
|
38
|
-
interface McpApiParams {
|
|
39
|
-
tool: string
|
|
40
|
-
docId?: string
|
|
41
|
-
query?: string
|
|
42
|
-
docSpaceId?: string
|
|
43
|
-
content?: string
|
|
44
|
-
format?: 'markdown' | 'mdx'
|
|
45
|
-
title?: string
|
|
46
|
-
parentDocId?: string
|
|
47
|
-
slug?: string
|
|
48
|
-
description?: string
|
|
49
|
-
previewImgURL?: string
|
|
50
|
-
hidden?: boolean
|
|
51
|
-
icon?: string
|
|
52
|
-
recursive?: boolean
|
|
53
|
-
name?: string
|
|
54
|
-
templateId?: string
|
|
55
|
-
snippetId?: string
|
|
56
|
-
variableId?: string
|
|
57
|
-
variableType?: 'variable' | 'glossary'
|
|
58
|
-
variableContent?: string
|
|
59
|
-
spaceTargetMap?: Record<string, string>
|
|
60
|
-
categoryName?: string
|
|
61
|
-
categoryId?: string
|
|
62
|
-
docSpaceGroupId?: string
|
|
63
|
-
isAiEnabled?: boolean
|
|
64
|
-
isReviewSystemEnabled?: boolean
|
|
65
|
-
isBranchingSystemEnabled?: boolean
|
|
66
|
-
editMode?: 'free_edits' | 'drafts_reviews' | 'branches_reviews'
|
|
67
|
-
confirmDeletion?: boolean
|
|
68
|
-
hostnamePart?: { hostname: string; hostnamePath: string }
|
|
69
|
-
spaceLinks?: { label: string; docSpaceId: string }[]
|
|
70
|
-
publicAccessControlPart?: Record<string, unknown>
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// A team key is not bound to a space, so the space travels per call instead of
|
|
74
|
-
// being packed into the credential like a space key is.
|
|
10
|
+
ErrorCode,
|
|
11
|
+
isJSONRPCErrorResponse,
|
|
12
|
+
isJSONRPCRequest,
|
|
13
|
+
isJSONRPCResultResponse,
|
|
14
|
+
type JSONRPCMessage,
|
|
15
|
+
type RequestId,
|
|
16
|
+
} from '@modelcontextprotocol/sdk/types.js'
|
|
17
|
+
|
|
18
|
+
// A team key is not bound to a space, so the space travels per call instead of being packed into the credential.
|
|
75
19
|
const TEAM_API_KEY_PREFIX = 'abteam_'
|
|
76
20
|
|
|
77
|
-
const
|
|
78
|
-
(process.env.API_KEY || '').startsWith(TEAM_API_KEY_PREFIX)
|
|
79
|
-
|
|
80
|
-
// Create an MCP server
|
|
81
|
-
const server = new McpServer({
|
|
82
|
-
name: process.env.API_KEY ? 'Archbee CMS MCP' : 'Archbee Portal MCP',
|
|
83
|
-
version: '2.7.0',
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
server.tool(
|
|
87
|
-
'get_all_docs',
|
|
88
|
-
'Read every document in the Archbee doc space, with its content, and each document carries the name of the space it belongs to (docSpaceName). Returns the whole space, so prefer search_query or search_title when looking for something specific.',
|
|
89
|
-
getAllDocsSchema,
|
|
90
|
-
async (args) => callMcpApi({ tool: 'get-all-docs', ...args })
|
|
91
|
-
)
|
|
92
|
-
|
|
93
|
-
server.tool(
|
|
94
|
-
'get_doc',
|
|
95
|
-
'Read one Archbee document by id and return its content as markdown, alongside the id and name of the space it lives in. Each embedded content snippet appears as a :::ContentSnippet{docRefId="…"} block (list_content_snippets returns its text by that id); keep it when writing the content back, or the snippet is removed from the document.',
|
|
96
|
-
getDocSchema,
|
|
97
|
-
async (args) => callMcpApi({ tool: 'get-doc', ...args })
|
|
98
|
-
)
|
|
99
|
-
|
|
100
|
-
server.tool(
|
|
101
|
-
'search_query',
|
|
102
|
-
'Search the Archbee doc space by document content and return the matching documents. This is the tool to use for answering a question from the documentation. An empty query returns every document.',
|
|
103
|
-
searchQuerySchema,
|
|
104
|
-
async (args) => callMcpApi({ tool: 'search-query', ...args })
|
|
105
|
-
)
|
|
106
|
-
|
|
107
|
-
server.tool(
|
|
108
|
-
'search_title',
|
|
109
|
-
'Search the Archbee doc space by document title. Use this when looking for a page by name; use search_query to search inside document content.',
|
|
110
|
-
searchTitleSchema,
|
|
111
|
-
async (args) => callMcpApi({ tool: 'search-title', ...args })
|
|
112
|
-
)
|
|
113
|
-
|
|
114
|
-
// Write tools. With a space key the target space comes from SPACE_ID; with a
|
|
115
|
-
// team key it comes from the docSpaceId argument, which the team-level tools
|
|
116
|
-
// below do not take because they act on the team, not on a space.
|
|
117
|
-
|
|
118
|
-
server.tool(
|
|
119
|
-
'create_doc',
|
|
120
|
-
"Create a new document in the Archbee doc space from Markdown or MDX content. Free Edits: the document is created with its content. Drafts & Reviews: it is created empty and the content is saved as its draft. Branches & Reviews: it is created and committed in a new branch. A draft or branch goes live only after someone reviews and merges it in Archbee, and the response's warning says so. The response's `id` is the new document's id.",
|
|
121
|
-
createDocSchema,
|
|
122
|
-
async (args) => callWriteApi({ tool: 'create-doc', ...args })
|
|
123
|
-
)
|
|
124
|
-
|
|
125
|
-
server.tool(
|
|
126
|
-
'update_doc',
|
|
127
|
-
"Update an existing Archbee document. Only the provided fields are changed. Free Edits: the change applies directly. Drafts & Reviews: saved as a draft. Branches & Reviews: saved and committed in a new branch. A draft or branch goes live only after someone reviews and merges it in Archbee, and the response's warning says so.",
|
|
128
|
-
updateDocSchema,
|
|
129
|
-
async (args) => callWriteApi({ tool: 'update-doc', ...args })
|
|
130
|
-
)
|
|
131
|
-
|
|
132
|
-
server.tool(
|
|
133
|
-
'delete_doc',
|
|
134
|
-
'Permanently delete an Archbee document. This is destructive and cannot be undone: the document is removed together with every file and image uploaded to it, and any documents nested underneath it are deleted as well. Ask the user to confirm before calling this tool.',
|
|
135
|
-
deleteDocSchema,
|
|
136
|
-
async (args) => callWriteApi({ tool: 'delete-doc', ...args })
|
|
137
|
-
)
|
|
138
|
-
|
|
139
|
-
server.tool(
|
|
140
|
-
'create_doc_space',
|
|
141
|
-
"Create a new doc space (documentation space) in the current Archbee team. The response's `id` is the new space's id.",
|
|
142
|
-
createDocSpaceSchema,
|
|
143
|
-
async (args) => callWriteApi({ tool: 'create-doc-space', ...args })
|
|
144
|
-
)
|
|
145
|
-
|
|
146
|
-
server.tool(
|
|
147
|
-
'update_doc_space',
|
|
148
|
-
'Update an Archbee doc space: rename it, or change its icon, hostname, space links, public access control or edit mode. Only the provided parts are changed, and at least one must be provided. The response echoes the space name and icon, and get_all_docs / get_doc report the name too, so a rename can be verified.',
|
|
149
|
-
updateDocSpaceSchema,
|
|
150
|
-
async (args) => callWriteApi({ tool: 'update-doc-space', ...args })
|
|
151
|
-
)
|
|
152
|
-
|
|
153
|
-
server.tool(
|
|
154
|
-
'delete_doc_space',
|
|
155
|
-
'Permanently delete an Archbee doc space together with every document inside it. This is destructive and cannot be undone. With a space API key the space deleted is the one SPACE_ID points at, so that key dies with it and no further tool call will work; with a team key it is the space named by docSpaceId. Ask the user to confirm before calling this tool.',
|
|
156
|
-
deleteDocSpaceSchema,
|
|
157
|
-
async (args) => callWriteApi({ tool: 'delete-doc-space', ...args })
|
|
158
|
-
)
|
|
159
|
-
|
|
160
|
-
server.tool(
|
|
161
|
-
'create_space_group',
|
|
162
|
-
"Create a new space group (collection of doc spaces) in the current Archbee team. Requires a team admin. The response's `id` is the new group's id.",
|
|
163
|
-
createSpaceGroupSchema,
|
|
164
|
-
async (args) => callWriteApi({ tool: 'create-space-group', ...args })
|
|
165
|
-
)
|
|
166
|
-
|
|
167
|
-
server.tool(
|
|
168
|
-
'delete_space_group',
|
|
169
|
-
'Delete a space group (collection of doc spaces) from the current Archbee team. Requires a team admin. The group must already be empty: move or delete every doc space it still contains first, otherwise the call is rejected.',
|
|
170
|
-
deleteSpaceGroupSchema,
|
|
171
|
-
async (args) => callWriteApi({ tool: 'delete-space-group', ...args })
|
|
172
|
-
)
|
|
173
|
-
|
|
174
|
-
server.tool(
|
|
175
|
-
'list_space_groups',
|
|
176
|
-
'List every space group in the Archbee organization together with the doc spaces it contains, each with its id and name. Use it to discover a docSpaceId before calling a space-level tool. Requires a team API key (abteam_...); a space key is rejected.',
|
|
177
|
-
listSpaceGroupsSchema,
|
|
178
|
-
async (args) => callMcpApi({ tool: 'list-space-groups', ...args })
|
|
179
|
-
)
|
|
180
|
-
|
|
181
|
-
server.tool(
|
|
182
|
-
'whoami',
|
|
183
|
-
'Show who the API key acts as: the user, the Archbee organization (id, name, plan), how many doc spaces it has and its space limit, and for a space key the space it is bound to. Call it to confirm which organization a key belongs to, or before create_doc_space to see whether the limit is reached.',
|
|
184
|
-
whoamiSchema,
|
|
185
|
-
async () => callMcpApi({ tool: 'whoami' })
|
|
186
|
-
)
|
|
187
|
-
|
|
188
|
-
server.tool(
|
|
189
|
-
'create_category',
|
|
190
|
-
'Create a category (a folder in the space navigation) in an Archbee doc space. A category holds documents but has no content of its own. To put a document inside it, pass the returned `id` as `parentDocId` on create_doc or update_doc.',
|
|
191
|
-
createCategorySchema,
|
|
192
|
-
async (args) => callWriteApi({ tool: 'create-category', ...args })
|
|
193
|
-
)
|
|
194
|
-
|
|
195
|
-
server.tool(
|
|
196
|
-
'delete_category',
|
|
197
|
-
'Delete a category from an Archbee doc space. The category must already be empty: move or delete the documents inside it first, otherwise the call is rejected. Deleting a category never deletes documents.',
|
|
198
|
-
deleteCategorySchema,
|
|
199
|
-
async (args) => callWriteApi({ tool: 'delete-category', ...args })
|
|
200
|
-
)
|
|
201
|
-
|
|
202
|
-
server.tool(
|
|
203
|
-
'list_templates',
|
|
204
|
-
'List the document templates saved in the Archbee organization (Reusables → Templates), each with its id, name and content as markdown. Use apply_template to create a document from one.',
|
|
205
|
-
listTemplatesSchema,
|
|
206
|
-
async () => callMcpApi({ tool: 'list-templates' })
|
|
207
|
-
)
|
|
208
|
-
|
|
209
|
-
server.tool(
|
|
210
|
-
'create_template',
|
|
211
|
-
"Save a new document template in the Archbee organization, from Markdown or MDX content. A template is a starting point for new documents; it is not itself a published page. If a template with the same name already exists, the existing one is returned (alreadyExists: true) instead of a duplicate being created — its content is kept, so a different body needs a new name. The response's `id` is the template's id.",
|
|
212
|
-
createTemplateSchema,
|
|
213
|
-
async (args) => callWriteApi({ tool: 'create-template', ...args })
|
|
214
|
-
)
|
|
215
|
-
|
|
216
|
-
server.tool(
|
|
217
|
-
'delete_template',
|
|
218
|
-
"Delete a document template from the Archbee organization. The template is moved to the organization's Archives space (Archived Docs in the app), where a person can restore it. Ask the user to confirm before calling this tool.",
|
|
219
|
-
deleteTemplateSchema,
|
|
220
|
-
async (args) => callWriteApi({ tool: 'delete-template', ...args })
|
|
221
|
-
)
|
|
222
|
-
|
|
223
|
-
server.tool(
|
|
224
|
-
'apply_template',
|
|
225
|
-
"Create a new Archbee document from a saved template. The template content is copied verbatim, including blocks markdown cannot express (API endpoints, diagrams). The template itself is unchanged. The response's `id` is the new document's id.",
|
|
226
|
-
applyTemplateSchema,
|
|
227
|
-
async (args) => callWriteApi({ tool: 'apply-template', ...args })
|
|
228
|
-
)
|
|
21
|
+
const DEFAULT_ARCHBEE_API_URL = 'https://api.archbee.com'
|
|
229
22
|
|
|
230
|
-
|
|
231
|
-
'list_content_snippets',
|
|
232
|
-
'List the reusable content snippets saved in the Archbee organization (Reusables → Content snippets), each with its id, name and content as markdown. A snippet is a block of content embedded into many documents and edited in one place.',
|
|
233
|
-
listContentSnippetsSchema,
|
|
234
|
-
async () => callMcpApi({ tool: 'list-content-snippets' })
|
|
235
|
-
)
|
|
23
|
+
const API_ADDRESS_HINT = `ARCHBEE_API_URL must be the API address, such as ${DEFAULT_ARCHBEE_API_URL}, not the app address`
|
|
236
24
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
async (args) => callWriteApi({ tool: 'create-content-snippet', ...args })
|
|
242
|
-
)
|
|
243
|
-
|
|
244
|
-
server.tool(
|
|
245
|
-
'update_content_snippet',
|
|
246
|
-
'Update a reusable content snippet: rename it, replace its content, or both. The change reaches every document the snippet is embedded in. At least one of name or content must be provided.',
|
|
247
|
-
updateContentSnippetSchema,
|
|
248
|
-
async (args) => callWriteApi({ tool: 'update-content-snippet', ...args })
|
|
249
|
-
)
|
|
250
|
-
|
|
251
|
-
server.tool(
|
|
252
|
-
'delete_content_snippet',
|
|
253
|
-
'Permanently delete a reusable content snippet from the Archbee organization. Documents that embed it lose that content. This cannot be undone — ask the user to confirm before calling this tool.',
|
|
254
|
-
deleteContentSnippetSchema,
|
|
255
|
-
async (args) => callWriteApi({ tool: 'delete-content-snippet', ...args })
|
|
256
|
-
)
|
|
257
|
-
|
|
258
|
-
server.tool(
|
|
259
|
-
'list_variables',
|
|
260
|
-
'List the content variables and glossary terms saved in the Archbee organization (Reusables → Variables / Glossary), each with its id, name, value and per-space or per-language overrides.',
|
|
261
|
-
listVariablesSchema,
|
|
262
|
-
async (args) => callMcpApi({ tool: 'list-variables', ...args })
|
|
263
|
-
)
|
|
264
|
-
|
|
265
|
-
server.tool(
|
|
266
|
-
'create_variable',
|
|
267
|
-
"Create a content variable (or a glossary term) in the Archbee organization. A variable is referenced by name inside document content and resolved at render time. For a glossary term, readers see variableContent as the term and description as its definition (required) — the name is only the reference key. Requires a Scaling or Enterprise subscription. The response's `id` is the new variable's id.",
|
|
268
|
-
createVariableSchema,
|
|
269
|
-
async (args) => callWriteApi({ tool: 'create-variable', ...args })
|
|
270
|
-
)
|
|
271
|
-
|
|
272
|
-
server.tool(
|
|
273
|
-
'update_variable',
|
|
274
|
-
'Update a content variable or glossary term. Only the provided fields change, and at least one must be provided. The new value reaches every document that references it.',
|
|
275
|
-
updateVariableSchema,
|
|
276
|
-
async (args) => callWriteApi({ tool: 'update-variable', ...args })
|
|
277
|
-
)
|
|
278
|
-
|
|
279
|
-
server.tool(
|
|
280
|
-
'delete_variable',
|
|
281
|
-
'Permanently delete a content variable or glossary term from the Archbee organization. Documents that reference it by name stop resolving. Ask the user to confirm before calling this tool.',
|
|
282
|
-
deleteVariableSchema,
|
|
283
|
-
async (args) => callWriteApi({ tool: 'delete-variable', ...args })
|
|
284
|
-
)
|
|
285
|
-
|
|
286
|
-
// Origin of the Archbee API the key belongs to, same variable the CLI takes.
|
|
287
|
-
// app.archbee.com/api is being phased out, so the default is api.archbee.com.
|
|
288
|
-
const apiUrl = (route: string): string | null => {
|
|
289
|
-
const apiUrlInput =
|
|
290
|
-
(process.env.ARCHBEE_API_URL || '').trim() || 'https://api.archbee.com'
|
|
291
|
-
|
|
292
|
-
try {
|
|
293
|
-
return new URL(route, apiUrlInput).toString()
|
|
294
|
-
} catch {
|
|
295
|
-
return null
|
|
296
|
-
}
|
|
25
|
+
interface IBridgeTarget {
|
|
26
|
+
url: URL
|
|
27
|
+
headers: Record<string, string>
|
|
28
|
+
notFoundMessage: string
|
|
297
29
|
}
|
|
298
30
|
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
throw new Error('Invalid ARCHBEE_API_URL format.')
|
|
304
|
-
}
|
|
31
|
+
class BridgeConfigError extends Error {}
|
|
32
|
+
|
|
33
|
+
const environmentValue = (name: string): string =>
|
|
34
|
+
(process.env[name] || '').trim()
|
|
305
35
|
|
|
306
|
-
|
|
36
|
+
const parseUrl = (value: string, errorMessage: string): URL => {
|
|
307
37
|
try {
|
|
308
|
-
|
|
38
|
+
return new URL(value)
|
|
309
39
|
} catch {
|
|
310
|
-
throw new
|
|
311
|
-
'Invalid API_BASE format. Give the full https:// link to the portal.'
|
|
312
|
-
)
|
|
40
|
+
throw new BridgeConfigError(errorMessage)
|
|
313
41
|
}
|
|
314
|
-
|
|
315
|
-
const docId = portal.pathname.replace(/\/+$/, '')
|
|
316
|
-
const response = await axios.get(lookupUrl, {
|
|
317
|
-
params: docId
|
|
318
|
-
? { hostname: portal.hostname, docId }
|
|
319
|
-
: { hostname: portal.hostname },
|
|
320
|
-
timeout: 30000,
|
|
321
|
-
})
|
|
322
|
-
|
|
323
|
-
const portalSpace: { id?: unknown; statusCode?: unknown } | undefined =
|
|
324
|
-
response.data?.data
|
|
325
|
-
const spaceId = portalSpace?.id
|
|
326
|
-
// An unknown path answers 404 with the root space's id and a protected space 403: neither is readable keyless
|
|
327
|
-
if (
|
|
328
|
-
portalSpace?.statusCode ||
|
|
329
|
-
typeof spaceId !== 'string' ||
|
|
330
|
-
!spaceId.startsWith('PUBLISHED-')
|
|
331
|
-
) {
|
|
332
|
-
throw new Error(
|
|
333
|
-
"No public Archbee portal found at API_BASE. Set SPACE_ID to the id in the portal's /.well-known/mcp.json instead."
|
|
334
|
-
)
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
return spaceId
|
|
338
42
|
}
|
|
339
43
|
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
portalSpaceIdLookup ??= resolvePortalSpaceId(portalUrl).catch(
|
|
345
|
-
(error: unknown) => {
|
|
346
|
-
portalSpaceIdLookup = null
|
|
347
|
-
throw error
|
|
348
|
-
}
|
|
44
|
+
const archbeeApiUrl = (route: string): URL => {
|
|
45
|
+
const apiOrigin = parseUrl(
|
|
46
|
+
environmentValue('ARCHBEE_API_URL') || DEFAULT_ARCHBEE_API_URL,
|
|
47
|
+
'Invalid ARCHBEE_API_URL format.'
|
|
349
48
|
)
|
|
350
|
-
return
|
|
49
|
+
return new URL(route, apiOrigin)
|
|
351
50
|
}
|
|
352
51
|
|
|
353
|
-
//
|
|
354
|
-
|
|
355
|
-
const
|
|
356
|
-
const
|
|
357
|
-
|
|
358
|
-
if (!apiKey) {
|
|
359
|
-
return ''
|
|
360
|
-
}
|
|
52
|
+
// API_KEY writes and reads as a member; without one, SPACE_ID or API_BASE reads one public docs site.
|
|
53
|
+
const resolveBridgeTarget = (): IBridgeTarget => {
|
|
54
|
+
const apiKey = environmentValue('API_KEY')
|
|
55
|
+
const spaceId = environmentValue('SPACE_ID')
|
|
56
|
+
const portalUrl = environmentValue('API_BASE')
|
|
361
57
|
|
|
362
|
-
if (
|
|
363
|
-
return
|
|
58
|
+
if (apiKey.startsWith(TEAM_API_KEY_PREFIX)) {
|
|
59
|
+
return {
|
|
60
|
+
url: archbeeApiUrl('/mcp'),
|
|
61
|
+
headers: { Authorization: `Bearer ${apiKey}` },
|
|
62
|
+
notFoundMessage: `No Archbee MCP server at this address: ${API_ADDRESS_HINT}.`,
|
|
63
|
+
}
|
|
364
64
|
}
|
|
365
65
|
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
const teamMode = usesTeamApiKey()
|
|
374
|
-
const docSpaceIdInput = teamMode
|
|
375
|
-
? params.docSpaceId || ''
|
|
376
|
-
: process.env.SPACE_ID || ''
|
|
377
|
-
|
|
378
|
-
if (!teamMode && !portalUrl && !docSpaceIdInput) {
|
|
66
|
+
if (apiKey) {
|
|
67
|
+
if (!spaceId) {
|
|
68
|
+
throw new BridgeConfigError(
|
|
69
|
+
'A space API_KEY needs SPACE_ID, the id of the space it belongs to.'
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
const credential = Buffer.from(`${spaceId}~${apiKey}`).toString('base64')
|
|
379
73
|
return {
|
|
380
|
-
|
|
381
|
-
|
|
74
|
+
url: archbeeApiUrl('/mcp'),
|
|
75
|
+
headers: { Authorization: `Bearer ${credential}` },
|
|
76
|
+
notFoundMessage: `No Archbee MCP server at this address: ${API_ADDRESS_HINT}.`,
|
|
382
77
|
}
|
|
383
78
|
}
|
|
384
79
|
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
if (urlMcp === null) {
|
|
80
|
+
if (spaceId) {
|
|
81
|
+
if (!spaceId.startsWith('PUBLISHED-')) {
|
|
82
|
+
throw new BridgeConfigError(
|
|
83
|
+
"Reads without an API_KEY take the PUBLISHED- id from the portal's /.well-known/mcp.json. A live space id needs an API_KEY."
|
|
84
|
+
)
|
|
85
|
+
}
|
|
392
86
|
return {
|
|
393
|
-
|
|
394
|
-
|
|
87
|
+
url: archbeeApiUrl(`/mcp/space/${encodeURIComponent(spaceId)}`),
|
|
88
|
+
headers: {},
|
|
89
|
+
notFoundMessage: `No public documentation found for this SPACE_ID: check SPACE_ID. ${API_ADDRESS_HINT}.`,
|
|
395
90
|
}
|
|
396
91
|
}
|
|
397
92
|
|
|
398
|
-
if (
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
93
|
+
if (portalUrl) {
|
|
94
|
+
const portal = parseUrl(
|
|
95
|
+
portalUrl,
|
|
96
|
+
'Invalid API_BASE format. Give the full https:// link to the docs site.'
|
|
97
|
+
)
|
|
98
|
+
portal.pathname = `${portal.pathname.replace(/\/+$/, '')}/mcp`
|
|
99
|
+
portal.search = ''
|
|
100
|
+
portal.hash = ''
|
|
403
101
|
return {
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
},
|
|
409
|
-
],
|
|
410
|
-
isError: true,
|
|
102
|
+
url: portal,
|
|
103
|
+
headers: {},
|
|
104
|
+
notFoundMessage:
|
|
105
|
+
'No public Archbee documentation found at this address: check API_BASE.',
|
|
411
106
|
}
|
|
412
107
|
}
|
|
413
108
|
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
params.docSpaceId = docSpaceId
|
|
419
|
-
}
|
|
109
|
+
throw new BridgeConfigError(
|
|
110
|
+
'Set API_KEY (plus SPACE_ID for a space key), or API_BASE with the link to a public docs site.'
|
|
111
|
+
)
|
|
112
|
+
}
|
|
420
113
|
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
114
|
+
// The SDK appends the response body to its message; the server's own reason is its error_description.
|
|
115
|
+
const serverReason = (error: StreamableHTTPError): string | undefined => {
|
|
116
|
+
try {
|
|
117
|
+
const body: unknown = JSON.parse(
|
|
118
|
+
error.message.slice(error.message.indexOf('{'))
|
|
119
|
+
)
|
|
120
|
+
const reason =
|
|
121
|
+
typeof body === 'object' && body !== null && 'error_description' in body
|
|
122
|
+
? body.error_description
|
|
123
|
+
: undefined
|
|
124
|
+
return typeof reason === 'string' ? reason : undefined
|
|
125
|
+
} catch {
|
|
126
|
+
return undefined
|
|
127
|
+
}
|
|
128
|
+
}
|
|
427
129
|
|
|
428
|
-
|
|
429
|
-
|
|
130
|
+
const describeRemoteError = (error: unknown, target: IBridgeTarget): string => {
|
|
131
|
+
if (error instanceof StreamableHTTPError) {
|
|
132
|
+
if (error.code === 401) {
|
|
133
|
+
return 'Authentication failed: check API_KEY and SPACE_ID.'
|
|
430
134
|
}
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
135
|
+
if (error.code === 403) {
|
|
136
|
+
return (
|
|
137
|
+
serverReason(error) ?? 'Access refused: check API_KEY and SPACE_ID.'
|
|
138
|
+
)
|
|
139
|
+
}
|
|
140
|
+
if (error.code === 404) {
|
|
141
|
+
return target.notFoundMessage
|
|
437
142
|
}
|
|
438
143
|
}
|
|
144
|
+
return error instanceof Error ? error.message : String(error)
|
|
439
145
|
}
|
|
440
146
|
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
:
|
|
147
|
+
const requestIdOf = (body: RequestInit['body']): RequestId | undefined => {
|
|
148
|
+
if (typeof body !== 'string') {
|
|
149
|
+
return undefined
|
|
150
|
+
}
|
|
151
|
+
try {
|
|
152
|
+
const message: unknown = JSON.parse(body)
|
|
153
|
+
return isJSONRPCRequest(message) ? message.id : undefined
|
|
154
|
+
} catch {
|
|
155
|
+
return undefined
|
|
156
|
+
}
|
|
157
|
+
}
|
|
447
158
|
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
159
|
+
const runBridge = async (target: IBridgeTarget): Promise<void> => {
|
|
160
|
+
const local = new StdioServerTransport()
|
|
161
|
+
let isClosing = false
|
|
162
|
+
const pendingRequestIds = new Set<RequestId>()
|
|
163
|
+
|
|
164
|
+
// Without an answer the client waits on the request until its own timeout.
|
|
165
|
+
const answerWithError = (id: RequestId, errorMessage: string): void => {
|
|
166
|
+
if (isClosing || !pendingRequestIds.delete(id)) {
|
|
167
|
+
return
|
|
457
168
|
}
|
|
169
|
+
void local.send({
|
|
170
|
+
jsonrpc: '2.0',
|
|
171
|
+
id,
|
|
172
|
+
error: { code: ErrorCode.InternalError, message: errorMessage },
|
|
173
|
+
})
|
|
458
174
|
}
|
|
459
175
|
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
content
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
},
|
|
470
|
-
],
|
|
471
|
-
isError: true,
|
|
176
|
+
// A tool answer streams back; the SDK reports a dropped stream only to onerror, which names no request.
|
|
177
|
+
const fetchWatchingAnswers: FetchLike = async (url, init) => {
|
|
178
|
+
const response = await fetch(url, init)
|
|
179
|
+
const requestId = requestIdOf(init?.body)
|
|
180
|
+
const isAnswerStream = response.headers
|
|
181
|
+
.get('content-type')
|
|
182
|
+
?.includes('text/event-stream')
|
|
183
|
+
if (requestId === undefined || !response.body || !isAnswerStream) {
|
|
184
|
+
return response
|
|
472
185
|
}
|
|
186
|
+
const reader = response.body.getReader()
|
|
187
|
+
const body = new ReadableStream<Uint8Array>({
|
|
188
|
+
pull: async (controller) => {
|
|
189
|
+
try {
|
|
190
|
+
const { done, value } = await reader.read()
|
|
191
|
+
if (done) {
|
|
192
|
+
controller.close()
|
|
193
|
+
} else {
|
|
194
|
+
controller.enqueue(value)
|
|
195
|
+
}
|
|
196
|
+
} catch (error) {
|
|
197
|
+
answerWithError(
|
|
198
|
+
requestId,
|
|
199
|
+
`The connection to Archbee dropped before the answer arrived. A write may or may not have been applied: check before retrying. (${error instanceof Error ? error.message : String(error)})`
|
|
200
|
+
)
|
|
201
|
+
controller.error(error)
|
|
202
|
+
}
|
|
203
|
+
},
|
|
204
|
+
cancel: (reason) => reader.cancel(reason),
|
|
205
|
+
})
|
|
206
|
+
return new Response(body, {
|
|
207
|
+
status: response.status,
|
|
208
|
+
statusText: response.statusText,
|
|
209
|
+
headers: response.headers,
|
|
210
|
+
})
|
|
473
211
|
}
|
|
474
212
|
|
|
475
|
-
|
|
476
|
-
}
|
|
213
|
+
const remote = new StreamableHTTPClientTransport(target.url, {
|
|
214
|
+
requestInit: { headers: target.headers },
|
|
215
|
+
fetch: fetchWatchingAnswers,
|
|
216
|
+
})
|
|
477
217
|
|
|
478
|
-
const
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
return 'Request timeout - the server took too long to respond.'
|
|
218
|
+
const shutdown = async (exitCode: number): Promise<void> => {
|
|
219
|
+
if (isClosing) {
|
|
220
|
+
return
|
|
482
221
|
}
|
|
483
|
-
|
|
484
|
-
|
|
222
|
+
isClosing = true
|
|
223
|
+
await Promise.allSettled([remote.close(), local.close()])
|
|
224
|
+
process.exit(exitCode)
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
local.onmessage = (message: JSONRPCMessage) => {
|
|
228
|
+
if (isJSONRPCRequest(message)) {
|
|
229
|
+
pendingRequestIds.add(message.id)
|
|
485
230
|
}
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
if (
|
|
489
|
-
|
|
490
|
-
}
|
|
491
|
-
if (status === 404) {
|
|
492
|
-
return 'Resource not found - check the document ID.'
|
|
493
|
-
}
|
|
494
|
-
if (status >= 500) {
|
|
495
|
-
return 'Server error - please try again later.'
|
|
231
|
+
// remote.onerror has already logged the failure.
|
|
232
|
+
remote.send(message).catch((error: unknown) => {
|
|
233
|
+
if (isJSONRPCRequest(message)) {
|
|
234
|
+
answerWithError(message.id, describeRemoteError(error, target))
|
|
496
235
|
}
|
|
497
|
-
|
|
498
|
-
}
|
|
499
|
-
return `Network error: ${error.message}`
|
|
500
|
-
}
|
|
501
|
-
if (error instanceof Error) {
|
|
502
|
-
return error.message
|
|
236
|
+
})
|
|
503
237
|
}
|
|
504
|
-
return 'An unexpected error occurred.'
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
const main = async () => {
|
|
508
|
-
const transport = new StdioServerTransport()
|
|
509
238
|
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
239
|
+
remote.onmessage = (message: JSONRPCMessage) => {
|
|
240
|
+
if (
|
|
241
|
+
(isJSONRPCResultResponse(message) || isJSONRPCErrorResponse(message)) &&
|
|
242
|
+
message.id !== undefined
|
|
243
|
+
) {
|
|
244
|
+
pendingRequestIds.delete(message.id)
|
|
245
|
+
}
|
|
246
|
+
// Later requests must carry the negotiated version in the mcp-protocol-version header.
|
|
247
|
+
if (
|
|
248
|
+
isJSONRPCResultResponse(message) &&
|
|
249
|
+
typeof message.result.protocolVersion === 'string'
|
|
250
|
+
) {
|
|
251
|
+
remote.setProtocolVersion(message.result.protocolVersion)
|
|
517
252
|
}
|
|
518
|
-
|
|
253
|
+
void local.send(message)
|
|
519
254
|
}
|
|
520
255
|
|
|
521
|
-
|
|
522
|
-
|
|
256
|
+
remote.onerror = (error: Error) => {
|
|
257
|
+
console.error(`Archbee MCP: ${describeRemoteError(error, target)}`)
|
|
258
|
+
}
|
|
259
|
+
local.onerror = (error: Error) => {
|
|
260
|
+
console.error(`Archbee MCP: ${error.message}`)
|
|
261
|
+
}
|
|
262
|
+
local.onclose = () => {
|
|
263
|
+
void shutdown(0)
|
|
264
|
+
}
|
|
265
|
+
// The client quitting closes stdin, which the stdio transport does not report.
|
|
266
|
+
process.stdin.on('end', () => void shutdown(0))
|
|
267
|
+
|
|
268
|
+
process.on('SIGINT', () => void shutdown(0))
|
|
269
|
+
process.on('SIGTERM', () => void shutdown(0))
|
|
270
|
+
|
|
271
|
+
await remote.start()
|
|
272
|
+
await local.start()
|
|
273
|
+
console.error(`Archbee MCP bridge connected to ${target.url.origin}`)
|
|
274
|
+
}
|
|
523
275
|
|
|
276
|
+
const main = async (): Promise<void> => {
|
|
524
277
|
try {
|
|
525
|
-
await
|
|
526
|
-
console.error('Archbee MCP server started successfully')
|
|
278
|
+
await runBridge(resolveBridgeTarget())
|
|
527
279
|
} catch (error) {
|
|
528
280
|
console.error(
|
|
529
|
-
|
|
530
|
-
error instanceof Error ? error.message : error
|
|
281
|
+
`Archbee MCP failed to start: ${error instanceof Error ? error.message : String(error)}`
|
|
531
282
|
)
|
|
532
283
|
process.exit(1)
|
|
533
284
|
}
|
|
534
285
|
}
|
|
535
286
|
|
|
536
|
-
main()
|
|
537
|
-
console.error('Fatal error:', error instanceof Error ? error.message : error)
|
|
538
|
-
process.exit(1)
|
|
539
|
-
})
|
|
287
|
+
void main()
|