@archbee/mcp 2.5.1 → 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 -212
- package/dist/index.js +180 -216
- package/dist/index.js.map +1 -1
- package/package.json +4 -5
- package/src/index.ts +221 -465
- package/dist/tool-schemas.js +0 -324
- package/dist/tool-schemas.js.map +0 -1
- package/src/tool-schemas.ts +0 -414
package/src/index.ts
CHANGED
|
@@ -1,531 +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
|
-
} from './tool-schemas'
|
|
36
|
-
|
|
37
|
-
interface McpApiParams {
|
|
38
|
-
tool: string
|
|
39
|
-
docId?: string
|
|
40
|
-
query?: string
|
|
41
|
-
docSpaceId?: string
|
|
42
|
-
content?: string
|
|
43
|
-
format?: 'markdown' | 'mdx'
|
|
44
|
-
title?: string
|
|
45
|
-
parentDocId?: string
|
|
46
|
-
slug?: string
|
|
47
|
-
description?: string
|
|
48
|
-
previewImgURL?: string
|
|
49
|
-
hidden?: boolean
|
|
50
|
-
icon?: string
|
|
51
|
-
recursive?: boolean
|
|
52
|
-
name?: string
|
|
53
|
-
templateId?: string
|
|
54
|
-
snippetId?: string
|
|
55
|
-
variableId?: string
|
|
56
|
-
variableType?: 'variable' | 'glossary'
|
|
57
|
-
variableContent?: string
|
|
58
|
-
spaceTargetMap?: Record<string, string>
|
|
59
|
-
categoryName?: string
|
|
60
|
-
categoryId?: string
|
|
61
|
-
docSpaceGroupId?: string
|
|
62
|
-
isAiEnabled?: boolean
|
|
63
|
-
isReviewSystemEnabled?: boolean
|
|
64
|
-
isBranchingSystemEnabled?: boolean
|
|
65
|
-
editMode?: 'free_edits' | 'drafts_reviews' | 'branches_reviews'
|
|
66
|
-
confirmDeletion?: boolean
|
|
67
|
-
hostnamePart?: { hostname: string; hostnamePath: string }
|
|
68
|
-
spaceLinks?: { label: string; docSpaceId: string }[]
|
|
69
|
-
publicAccessControlPart?: Record<string, unknown>
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// A team key is not bound to a space, so the space travels per call instead of
|
|
73
|
-
// 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.
|
|
74
19
|
const TEAM_API_KEY_PREFIX = 'abteam_'
|
|
75
20
|
|
|
76
|
-
const
|
|
77
|
-
(process.env.API_KEY || '').startsWith(TEAM_API_KEY_PREFIX)
|
|
78
|
-
|
|
79
|
-
// Create an MCP server
|
|
80
|
-
const server = new McpServer({
|
|
81
|
-
name: process.env.API_KEY ? 'Archbee CMS MCP' : 'Archbee Portal MCP',
|
|
82
|
-
version: '2.5.1',
|
|
83
|
-
})
|
|
84
|
-
|
|
85
|
-
server.tool(
|
|
86
|
-
'get_all_docs',
|
|
87
|
-
'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.',
|
|
88
|
-
getAllDocsSchema,
|
|
89
|
-
async (args) => callMcpApi({ tool: 'get-all-docs', ...args })
|
|
90
|
-
)
|
|
91
|
-
|
|
92
|
-
server.tool(
|
|
93
|
-
'get_doc',
|
|
94
|
-
'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.',
|
|
95
|
-
getDocSchema,
|
|
96
|
-
async (args) => callMcpApi({ tool: 'get-doc', ...args })
|
|
97
|
-
)
|
|
98
|
-
|
|
99
|
-
server.tool(
|
|
100
|
-
'search_query',
|
|
101
|
-
'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.',
|
|
102
|
-
searchQuerySchema,
|
|
103
|
-
async (args) => callMcpApi({ tool: 'search-query', ...args })
|
|
104
|
-
)
|
|
105
|
-
|
|
106
|
-
server.tool(
|
|
107
|
-
'search_title',
|
|
108
|
-
'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.',
|
|
109
|
-
searchTitleSchema,
|
|
110
|
-
async (args) => callMcpApi({ tool: 'search-title', ...args })
|
|
111
|
-
)
|
|
112
|
-
|
|
113
|
-
// Write tools. With a space key the target space comes from SPACE_ID; with a
|
|
114
|
-
// team key it comes from the docSpaceId argument, which the team-level tools
|
|
115
|
-
// below do not take because they act on the team, not on a space.
|
|
116
|
-
|
|
117
|
-
server.tool(
|
|
118
|
-
'create_doc',
|
|
119
|
-
"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.",
|
|
120
|
-
createDocSchema,
|
|
121
|
-
async (args) => callWriteApi({ tool: 'create-doc', ...args })
|
|
122
|
-
)
|
|
123
|
-
|
|
124
|
-
server.tool(
|
|
125
|
-
'update_doc',
|
|
126
|
-
"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.",
|
|
127
|
-
updateDocSchema,
|
|
128
|
-
async (args) => callWriteApi({ tool: 'update-doc', ...args })
|
|
129
|
-
)
|
|
130
|
-
|
|
131
|
-
server.tool(
|
|
132
|
-
'delete_doc',
|
|
133
|
-
'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.',
|
|
134
|
-
deleteDocSchema,
|
|
135
|
-
async (args) => callWriteApi({ tool: 'delete-doc', ...args })
|
|
136
|
-
)
|
|
137
|
-
|
|
138
|
-
server.tool(
|
|
139
|
-
'create_doc_space',
|
|
140
|
-
'Create a new doc space (documentation space) in the current Archbee team.',
|
|
141
|
-
createDocSpaceSchema,
|
|
142
|
-
async (args) => callWriteApi({ tool: 'create-doc-space', ...args })
|
|
143
|
-
)
|
|
144
|
-
|
|
145
|
-
server.tool(
|
|
146
|
-
'update_doc_space',
|
|
147
|
-
'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.',
|
|
148
|
-
updateDocSpaceSchema,
|
|
149
|
-
async (args) => callWriteApi({ tool: 'update-doc-space', ...args })
|
|
150
|
-
)
|
|
151
|
-
|
|
152
|
-
server.tool(
|
|
153
|
-
'delete_doc_space',
|
|
154
|
-
'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.',
|
|
155
|
-
deleteDocSpaceSchema,
|
|
156
|
-
async (args) => callWriteApi({ tool: 'delete-doc-space', ...args })
|
|
157
|
-
)
|
|
158
|
-
|
|
159
|
-
server.tool(
|
|
160
|
-
'create_space_group',
|
|
161
|
-
'Create a new space group (collection of doc spaces) in the current Archbee team. Requires a team admin.',
|
|
162
|
-
createSpaceGroupSchema,
|
|
163
|
-
async (args) => callWriteApi({ tool: 'create-space-group', ...args })
|
|
164
|
-
)
|
|
165
|
-
|
|
166
|
-
server.tool(
|
|
167
|
-
'delete_space_group',
|
|
168
|
-
'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.',
|
|
169
|
-
deleteSpaceGroupSchema,
|
|
170
|
-
async (args) => callWriteApi({ tool: 'delete-space-group', ...args })
|
|
171
|
-
)
|
|
172
|
-
|
|
173
|
-
server.tool(
|
|
174
|
-
'list_space_groups',
|
|
175
|
-
'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.',
|
|
176
|
-
listSpaceGroupsSchema,
|
|
177
|
-
async (args) => callMcpApi({ tool: 'list-space-groups', ...args })
|
|
178
|
-
)
|
|
179
|
-
|
|
180
|
-
server.tool(
|
|
181
|
-
'create_category',
|
|
182
|
-
'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 categoryId as `parentDocId` on create_doc or update_doc.',
|
|
183
|
-
createCategorySchema,
|
|
184
|
-
async (args) => callWriteApi({ tool: 'create-category', ...args })
|
|
185
|
-
)
|
|
186
|
-
|
|
187
|
-
server.tool(
|
|
188
|
-
'delete_category',
|
|
189
|
-
'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.',
|
|
190
|
-
deleteCategorySchema,
|
|
191
|
-
async (args) => callWriteApi({ tool: 'delete-category', ...args })
|
|
192
|
-
)
|
|
193
|
-
|
|
194
|
-
server.tool(
|
|
195
|
-
'list_templates',
|
|
196
|
-
'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.',
|
|
197
|
-
listTemplatesSchema,
|
|
198
|
-
async () => callMcpApi({ tool: 'list-templates' })
|
|
199
|
-
)
|
|
200
|
-
|
|
201
|
-
server.tool(
|
|
202
|
-
'create_template',
|
|
203
|
-
'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.',
|
|
204
|
-
createTemplateSchema,
|
|
205
|
-
async (args) => callWriteApi({ tool: 'create-template', ...args })
|
|
206
|
-
)
|
|
207
|
-
|
|
208
|
-
server.tool(
|
|
209
|
-
'delete_template',
|
|
210
|
-
"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.",
|
|
211
|
-
deleteTemplateSchema,
|
|
212
|
-
async (args) => callWriteApi({ tool: 'delete-template', ...args })
|
|
213
|
-
)
|
|
214
|
-
|
|
215
|
-
server.tool(
|
|
216
|
-
'apply_template',
|
|
217
|
-
'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.',
|
|
218
|
-
applyTemplateSchema,
|
|
219
|
-
async (args) => callWriteApi({ tool: 'apply-template', ...args })
|
|
220
|
-
)
|
|
221
|
-
|
|
222
|
-
server.tool(
|
|
223
|
-
'list_content_snippets',
|
|
224
|
-
'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.',
|
|
225
|
-
listContentSnippetsSchema,
|
|
226
|
-
async () => callMcpApi({ tool: 'list-content-snippets' })
|
|
227
|
-
)
|
|
21
|
+
const DEFAULT_ARCHBEE_API_URL = 'https://api.archbee.com'
|
|
228
22
|
|
|
229
|
-
|
|
230
|
-
'create_content_snippet',
|
|
231
|
-
'Create a reusable content snippet in the Archbee organization, from Markdown or MDX content. Requires a Scaling or Enterprise subscription. If a snippet with the same name already exists, the existing one is returned (alreadyExists: true) instead of a duplicate being created — its content is kept, use update_content_snippet to change it.',
|
|
232
|
-
createContentSnippetSchema,
|
|
233
|
-
async (args) => callWriteApi({ tool: 'create-content-snippet', ...args })
|
|
234
|
-
)
|
|
23
|
+
const API_ADDRESS_HINT = `ARCHBEE_API_URL must be the API address, such as ${DEFAULT_ARCHBEE_API_URL}, not the app address`
|
|
235
24
|
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
)
|
|
242
|
-
|
|
243
|
-
server.tool(
|
|
244
|
-
'delete_content_snippet',
|
|
245
|
-
'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.',
|
|
246
|
-
deleteContentSnippetSchema,
|
|
247
|
-
async (args) => callWriteApi({ tool: 'delete-content-snippet', ...args })
|
|
248
|
-
)
|
|
249
|
-
|
|
250
|
-
server.tool(
|
|
251
|
-
'list_variables',
|
|
252
|
-
'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.',
|
|
253
|
-
listVariablesSchema,
|
|
254
|
-
async (args) => callMcpApi({ tool: 'list-variables', ...args })
|
|
255
|
-
)
|
|
256
|
-
|
|
257
|
-
server.tool(
|
|
258
|
-
'create_variable',
|
|
259
|
-
'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.',
|
|
260
|
-
createVariableSchema,
|
|
261
|
-
async (args) => callWriteApi({ tool: 'create-variable', ...args })
|
|
262
|
-
)
|
|
263
|
-
|
|
264
|
-
server.tool(
|
|
265
|
-
'update_variable',
|
|
266
|
-
'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.',
|
|
267
|
-
updateVariableSchema,
|
|
268
|
-
async (args) => callWriteApi({ tool: 'update-variable', ...args })
|
|
269
|
-
)
|
|
25
|
+
interface IBridgeTarget {
|
|
26
|
+
url: URL
|
|
27
|
+
headers: Record<string, string>
|
|
28
|
+
notFoundMessage: string
|
|
29
|
+
}
|
|
270
30
|
|
|
271
|
-
|
|
272
|
-
'delete_variable',
|
|
273
|
-
'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.',
|
|
274
|
-
deleteVariableSchema,
|
|
275
|
-
async (args) => callWriteApi({ tool: 'delete-variable', ...args })
|
|
276
|
-
)
|
|
31
|
+
class BridgeConfigError extends Error {}
|
|
277
32
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
const apiUrl = (route: string): string | null => {
|
|
281
|
-
const apiUrlInput =
|
|
282
|
-
(process.env.ARCHBEE_API_URL || '').trim() || 'https://api.archbee.com'
|
|
33
|
+
const environmentValue = (name: string): string =>
|
|
34
|
+
(process.env[name] || '').trim()
|
|
283
35
|
|
|
36
|
+
const parseUrl = (value: string, errorMessage: string): URL => {
|
|
284
37
|
try {
|
|
285
|
-
return new URL(
|
|
38
|
+
return new URL(value)
|
|
286
39
|
} catch {
|
|
287
|
-
|
|
40
|
+
throw new BridgeConfigError(errorMessage)
|
|
288
41
|
}
|
|
289
42
|
}
|
|
290
43
|
|
|
291
|
-
|
|
292
|
-
const
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
throw new Error('Invalid ARCHBEE_API_URL format.')
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
let portal: URL
|
|
299
|
-
try {
|
|
300
|
-
portal = new URL(portalUrl)
|
|
301
|
-
} catch {
|
|
302
|
-
throw new Error(
|
|
303
|
-
'Invalid API_BASE format. Give the full https:// link to the portal.'
|
|
304
|
-
)
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
const docId = portal.pathname.replace(/\/+$/, '')
|
|
308
|
-
const response = await axios.get(lookupUrl, {
|
|
309
|
-
params: docId
|
|
310
|
-
? { hostname: portal.hostname, docId }
|
|
311
|
-
: { hostname: portal.hostname },
|
|
312
|
-
timeout: 30000,
|
|
313
|
-
})
|
|
314
|
-
|
|
315
|
-
const portalSpace: { id?: unknown; statusCode?: unknown } | undefined =
|
|
316
|
-
response.data?.data
|
|
317
|
-
const spaceId = portalSpace?.id
|
|
318
|
-
// An unknown path answers 404 with the root space's id and a protected space 403: neither is readable keyless
|
|
319
|
-
if (
|
|
320
|
-
portalSpace?.statusCode ||
|
|
321
|
-
typeof spaceId !== 'string' ||
|
|
322
|
-
!spaceId.startsWith('PUBLISHED-')
|
|
323
|
-
) {
|
|
324
|
-
throw new Error(
|
|
325
|
-
"No public Archbee portal found at API_BASE. Set SPACE_ID to the id in the portal's /.well-known/mcp.json instead."
|
|
326
|
-
)
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
return spaceId
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
let portalSpaceIdLookup: Promise<string> | null = null
|
|
333
|
-
|
|
334
|
-
// One lookup per process; a failed one is dropped so the next call retries.
|
|
335
|
-
const portalSpaceId = (portalUrl: string): Promise<string> => {
|
|
336
|
-
portalSpaceIdLookup ??= resolvePortalSpaceId(portalUrl).catch(
|
|
337
|
-
(error: unknown) => {
|
|
338
|
-
portalSpaceIdLookup = null
|
|
339
|
-
throw error
|
|
340
|
-
}
|
|
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.'
|
|
341
48
|
)
|
|
342
|
-
return
|
|
49
|
+
return new URL(route, apiOrigin)
|
|
343
50
|
}
|
|
344
51
|
|
|
345
|
-
//
|
|
346
|
-
|
|
347
|
-
const
|
|
348
|
-
const
|
|
349
|
-
|
|
350
|
-
if (!apiKey) {
|
|
351
|
-
return ''
|
|
352
|
-
}
|
|
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')
|
|
353
57
|
|
|
354
|
-
if (
|
|
355
|
-
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
|
+
}
|
|
356
64
|
}
|
|
357
65
|
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
const teamMode = usesTeamApiKey()
|
|
366
|
-
const docSpaceIdInput = teamMode
|
|
367
|
-
? params.docSpaceId || ''
|
|
368
|
-
: process.env.SPACE_ID || ''
|
|
369
|
-
|
|
370
|
-
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')
|
|
371
73
|
return {
|
|
372
|
-
|
|
373
|
-
|
|
74
|
+
url: archbeeApiUrl('/mcp'),
|
|
75
|
+
headers: { Authorization: `Bearer ${credential}` },
|
|
76
|
+
notFoundMessage: `No Archbee MCP server at this address: ${API_ADDRESS_HINT}.`,
|
|
374
77
|
}
|
|
375
78
|
}
|
|
376
79
|
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
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
|
+
}
|
|
384
86
|
return {
|
|
385
|
-
|
|
386
|
-
|
|
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}.`,
|
|
387
90
|
}
|
|
388
91
|
}
|
|
389
92
|
|
|
390
|
-
if (
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
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 = ''
|
|
395
101
|
return {
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
},
|
|
401
|
-
],
|
|
402
|
-
isError: true,
|
|
102
|
+
url: portal,
|
|
103
|
+
headers: {},
|
|
104
|
+
notFoundMessage:
|
|
105
|
+
'No public Archbee documentation found at this address: check API_BASE.',
|
|
403
106
|
}
|
|
404
107
|
}
|
|
405
108
|
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
params.docSpaceId = docSpaceId
|
|
411
|
-
}
|
|
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
|
+
}
|
|
412
113
|
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
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
|
+
}
|
|
419
129
|
|
|
420
|
-
|
|
421
|
-
|
|
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.'
|
|
422
134
|
}
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
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
|
|
429
142
|
}
|
|
430
143
|
}
|
|
144
|
+
return error instanceof Error ? error.message : String(error)
|
|
431
145
|
}
|
|
432
146
|
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
:
|
|
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
|
+
}
|
|
439
158
|
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
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
|
|
449
168
|
}
|
|
169
|
+
void local.send({
|
|
170
|
+
jsonrpc: '2.0',
|
|
171
|
+
id,
|
|
172
|
+
error: { code: ErrorCode.InternalError, message: errorMessage },
|
|
173
|
+
})
|
|
450
174
|
}
|
|
451
175
|
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
content
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
},
|
|
462
|
-
],
|
|
463
|
-
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
|
|
464
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
|
+
})
|
|
465
211
|
}
|
|
466
212
|
|
|
467
|
-
|
|
468
|
-
}
|
|
213
|
+
const remote = new StreamableHTTPClientTransport(target.url, {
|
|
214
|
+
requestInit: { headers: target.headers },
|
|
215
|
+
fetch: fetchWatchingAnswers,
|
|
216
|
+
})
|
|
469
217
|
|
|
470
|
-
const
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
return 'Request timeout - the server took too long to respond.'
|
|
218
|
+
const shutdown = async (exitCode: number): Promise<void> => {
|
|
219
|
+
if (isClosing) {
|
|
220
|
+
return
|
|
474
221
|
}
|
|
475
|
-
|
|
476
|
-
|
|
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)
|
|
477
230
|
}
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
if (
|
|
481
|
-
|
|
482
|
-
}
|
|
483
|
-
if (status === 404) {
|
|
484
|
-
return 'Resource not found - check the document ID.'
|
|
485
|
-
}
|
|
486
|
-
if (status >= 500) {
|
|
487
|
-
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))
|
|
488
235
|
}
|
|
489
|
-
|
|
490
|
-
}
|
|
491
|
-
return `Network error: ${error.message}`
|
|
492
|
-
}
|
|
493
|
-
if (error instanceof Error) {
|
|
494
|
-
return error.message
|
|
236
|
+
})
|
|
495
237
|
}
|
|
496
|
-
return 'An unexpected error occurred.'
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
const main = async () => {
|
|
500
|
-
const transport = new StdioServerTransport()
|
|
501
238
|
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
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)
|
|
509
252
|
}
|
|
510
|
-
|
|
253
|
+
void local.send(message)
|
|
511
254
|
}
|
|
512
255
|
|
|
513
|
-
|
|
514
|
-
|
|
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
|
+
}
|
|
515
275
|
|
|
276
|
+
const main = async (): Promise<void> => {
|
|
516
277
|
try {
|
|
517
|
-
await
|
|
518
|
-
console.error('Archbee MCP server started successfully')
|
|
278
|
+
await runBridge(resolveBridgeTarget())
|
|
519
279
|
} catch (error) {
|
|
520
280
|
console.error(
|
|
521
|
-
|
|
522
|
-
error instanceof Error ? error.message : error
|
|
281
|
+
`Archbee MCP failed to start: ${error instanceof Error ? error.message : String(error)}`
|
|
523
282
|
)
|
|
524
283
|
process.exit(1)
|
|
525
284
|
}
|
|
526
285
|
}
|
|
527
286
|
|
|
528
|
-
main()
|
|
529
|
-
console.error('Fatal error:', error instanceof Error ? error.message : error)
|
|
530
|
-
process.exit(1)
|
|
531
|
-
})
|
|
287
|
+
void main()
|