@webpieces/mcp-server 0.4.789 → 0.4.791
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 +43 -17
- package/package.json +6 -6
- package/src/McpApiDispatcher.js +6 -0
- package/src/McpApiDispatcher.js.map +1 -1
- package/src/McpAuth.d.ts +4 -12
- package/src/McpAuth.js +4 -20
- package/src/McpAuth.js.map +1 -1
- package/src/McpRegistry.d.ts +40 -0
- package/src/McpRegistry.js +54 -0
- package/src/McpRegistry.js.map +1 -0
- package/src/McpToolCallRendering.d.ts +111 -0
- package/src/McpToolCallRendering.js +135 -0
- package/src/McpToolCallRendering.js.map +1 -0
- package/src/WpMcpErrorTranslator.d.ts +12 -100
- package/src/WpMcpErrorTranslator.js +28 -158
- package/src/WpMcpErrorTranslator.js.map +1 -1
- package/src/WpMcpServer.d.ts +7 -2
- package/src/WpMcpServer.js +27 -14
- package/src/WpMcpServer.js.map +1 -1
- package/src/__tests__/LegacyMcpHttpTestHarness.d.ts +24 -0
- package/src/__tests__/LegacyMcpHttpTestHarness.js +69 -0
- package/src/__tests__/LegacyMcpHttpTestHarness.js.map +1 -0
- package/src/__tests__/McpRemoteFixtures.d.ts +4 -3
- package/src/__tests__/McpRemoteFixtures.js +11 -11
- package/src/__tests__/McpRemoteFixtures.js.map +1 -1
- package/src/__tests__/WpMcpServerTestFixtures.d.ts +6 -0
- package/src/__tests__/WpMcpServerTestFixtures.js +7 -1
- package/src/__tests__/WpMcpServerTestFixtures.js.map +1 -1
- package/src/index.d.ts +4 -2
- package/src/index.js +12 -8
- package/src/index.js.map +1 -1
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { CallToolResult } from '@modelcontextprotocol/server';
|
|
2
|
+
import { ContextKey } from '@webpieces/core-util';
|
|
3
|
+
/** `_meta` key carrying the Webpieces requestId on every tools/call result, success or failure. */
|
|
4
|
+
export declare const MCP_REQUEST_ID_META_KEY = "webpieces/requestId";
|
|
5
|
+
/** JSON-RPC error `data` so a user can quote the requestId of any protocol-level failure. */
|
|
6
|
+
export declare class McpErrorData {
|
|
7
|
+
readonly requestId: string;
|
|
8
|
+
constructor(requestId: string);
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The two per-request facts a renderer needs that are NOT the error: the JSON-RPC `id` it must echo,
|
|
12
|
+
* and the tool being called. They are stamped into the request scope by `WpMcpServer` rather than
|
|
13
|
+
* threaded through every renderer signature, because `requestId` already travels that way
|
|
14
|
+
* ({@link WebpiecesCoreHeaders.REQUEST_ID}) and a second, parallel correlation parameter was a COPY
|
|
15
|
+
* of the context — which is what the deleted `McpFailureScope` was.
|
|
16
|
+
*
|
|
17
|
+
* Context-only and never logged: the logging backends read `RequestContext` for correlation on every
|
|
18
|
+
* record already, and this object exists purely so the renderers can be one-argument functions.
|
|
19
|
+
*/
|
|
20
|
+
export declare class McpCorrelation {
|
|
21
|
+
readonly jsonRpcId: string | number | null;
|
|
22
|
+
readonly toolName?: string | undefined;
|
|
23
|
+
static readonly KEY: ContextKey<McpCorrelation, "untrusted">;
|
|
24
|
+
constructor(jsonRpcId: string | number | null, toolName?: string | undefined);
|
|
25
|
+
/** Stamp what this request knows so far; `WpMcpServer` re-stamps once the tool name is known. */
|
|
26
|
+
static stamp(correlation: McpCorrelation): void;
|
|
27
|
+
/** A renderer reached before anything stamped (a failure with no parseable body) still renders. */
|
|
28
|
+
static current(): McpCorrelation;
|
|
29
|
+
/** The webpieces requestId of the request being served, as every MCP reply reports it. */
|
|
30
|
+
static requestId(): string;
|
|
31
|
+
}
|
|
32
|
+
/** The only error shape rendered into model-visible MCP tool content. */
|
|
33
|
+
export declare class ModelVisibleToolError {
|
|
34
|
+
readonly kind: string;
|
|
35
|
+
readonly message: string;
|
|
36
|
+
readonly requestId: string;
|
|
37
|
+
readonly field?: string | undefined;
|
|
38
|
+
readonly callerMessage?: string | undefined;
|
|
39
|
+
readonly errorCode?: string | undefined;
|
|
40
|
+
readonly retryAfterSeconds?: number | undefined;
|
|
41
|
+
readonly statusCode?: number | undefined;
|
|
42
|
+
constructor(kind: string, message: string, requestId: string, field?: string | undefined, callerMessage?: string | undefined, errorCode?: string | undefined, retryAfterSeconds?: number | undefined, statusCode?: number | undefined);
|
|
43
|
+
}
|
|
44
|
+
/** JSON-RPC error body written by the HTTP boundary before the SDK is involved. */
|
|
45
|
+
export declare class McpHttpErrorBody {
|
|
46
|
+
readonly error: McpHttpErrorDetail;
|
|
47
|
+
readonly id: string | number | null;
|
|
48
|
+
readonly jsonrpc = "2.0";
|
|
49
|
+
constructor(error: McpHttpErrorDetail, id: string | number | null);
|
|
50
|
+
}
|
|
51
|
+
export declare class McpHttpErrorDetail {
|
|
52
|
+
readonly code: number;
|
|
53
|
+
readonly message: string;
|
|
54
|
+
readonly data: McpErrorData;
|
|
55
|
+
constructor(code: number, message: string, data: McpErrorData);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The application's own `tools/call` error translation — the MCP twin of the `ErrorTranslator` an app
|
|
59
|
+
* registers on `ClientRegistry` for HTTP and the `IpcErrorTranslator` it registers on `IpcRegistry`.
|
|
60
|
+
* Register one with {@link McpRegistry.setErrorTranslator}.
|
|
61
|
+
*
|
|
62
|
+
* An app owns its error taxonomy and owns how those errors should be explained to a model, so a
|
|
63
|
+
* claimed error yields the ENTIRE `CallToolResult` — content, `structuredContent`, `isError`, the lot.
|
|
64
|
+
*
|
|
65
|
+
* # There is NO `fromWire`, and that is the protocol, not an oversight
|
|
66
|
+
*
|
|
67
|
+
* The HTTP and IPC translators have two halves because webpieces sits on BOTH ends of those wires: it
|
|
68
|
+
* writes the response AND, in another process, reads one. **webpieces is never the MCP client.** The
|
|
69
|
+
* client is Claude (or another model host); there is no return path for webpieces to translate, so
|
|
70
|
+
* the pair is one-directional by nature. Do not "restore the symmetry" by adding a half with nothing
|
|
71
|
+
* to call it.
|
|
72
|
+
*
|
|
73
|
+
* # The pre-SDK boundary does NOT share the HTTP `ErrorTranslator`, despite the identical type
|
|
74
|
+
*
|
|
75
|
+
* `WpMcpErrorTranslator.toBearerBoundaryResponse` returns an `HttpResponseDto`, exactly like
|
|
76
|
+
* `ErrorTranslator.toWire` — and it must STILL not be routed through it. Two reasons, both hard:
|
|
77
|
+
*
|
|
78
|
+
* - its body must stay JSON-RPC shaped (`McpHttpErrorBody`) or an MCP client cannot parse the reply
|
|
79
|
+
* at all, whereas an app's HTTP translator publishes the app's own REST envelope;
|
|
80
|
+
* - it emits the `401 + WWW-Authenticate: Bearer resource_metadata=...` that MCP clients use for OAuth
|
|
81
|
+
* discovery, so an app rewriting it breaks connector onboarding.
|
|
82
|
+
*
|
|
83
|
+
* Same TYPE, different OWNER. `tools/list` is framework-owned for the same reason.
|
|
84
|
+
*
|
|
85
|
+
* Contract:
|
|
86
|
+
* - `error` is the RAW thrown value: an app must be able to `instanceof` its own error classes.
|
|
87
|
+
* `ApiErrorBoundary` never substitutes an object for the thrown error, so there is exactly one
|
|
88
|
+
* error value on this path.
|
|
89
|
+
* - There is no "not mine" return. A registered translator REPLACES the webpieces default and
|
|
90
|
+
* declines by DELEGATING to it: `new McpDefaultToolCallRenderer().toWire(error)`.
|
|
91
|
+
* - webpieces default-fills `_meta['webpieces/requestId']` only when the returned result has NO
|
|
92
|
+
* `_meta`. An app that sets `_meta` owns it untouched.
|
|
93
|
+
* - scope is `tools/call` ONLY.
|
|
94
|
+
*/
|
|
95
|
+
export interface McpErrorTranslator {
|
|
96
|
+
/** An error in, the whole `tools/call` reply out. Named for its HTTP and IPC twins. */
|
|
97
|
+
toWire(error: Error): CallToolResult;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* webpieces' DEFAULT `tools/call` rendering, and what {@link McpRegistry} holds until an app installs
|
|
101
|
+
* its own. Public so an app's own {@link McpErrorTranslator} can DECLINE an error by delegating to it
|
|
102
|
+
* — the MCP twin of `WebpiecesDefaultErrorTranslator.toWire` on the HTTP side.
|
|
103
|
+
*
|
|
104
|
+
* Published text comes only from {@link ApiErrorBoundary.encode}: this never puts an error's own
|
|
105
|
+
* message in front of a model unless it is an `ApiEndUserError`.
|
|
106
|
+
*/
|
|
107
|
+
export declare class McpDefaultToolCallRenderer implements McpErrorTranslator {
|
|
108
|
+
private readonly boundary;
|
|
109
|
+
toWire(error: Error): CallToolResult;
|
|
110
|
+
private modelVisible;
|
|
111
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.McpDefaultToolCallRenderer = exports.McpHttpErrorDetail = exports.McpHttpErrorBody = exports.ModelVisibleToolError = exports.McpCorrelation = exports.McpErrorData = exports.MCP_REQUEST_ID_META_KEY = void 0;
|
|
4
|
+
const core_util_1 = require("@webpieces/core-util");
|
|
5
|
+
const core_context_1 = require("@webpieces/core-context");
|
|
6
|
+
/** `_meta` key carrying the Webpieces requestId on every tools/call result, success or failure. */
|
|
7
|
+
exports.MCP_REQUEST_ID_META_KEY = 'webpieces/requestId';
|
|
8
|
+
/** JSON-RPC error `data` so a user can quote the requestId of any protocol-level failure. */
|
|
9
|
+
class McpErrorData {
|
|
10
|
+
requestId;
|
|
11
|
+
constructor(requestId) {
|
|
12
|
+
this.requestId = requestId;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.McpErrorData = McpErrorData;
|
|
16
|
+
/**
|
|
17
|
+
* The two per-request facts a renderer needs that are NOT the error: the JSON-RPC `id` it must echo,
|
|
18
|
+
* and the tool being called. They are stamped into the request scope by `WpMcpServer` rather than
|
|
19
|
+
* threaded through every renderer signature, because `requestId` already travels that way
|
|
20
|
+
* ({@link WebpiecesCoreHeaders.REQUEST_ID}) and a second, parallel correlation parameter was a COPY
|
|
21
|
+
* of the context — which is what the deleted `McpFailureScope` was.
|
|
22
|
+
*
|
|
23
|
+
* Context-only and never logged: the logging backends read `RequestContext` for correlation on every
|
|
24
|
+
* record already, and this object exists purely so the renderers can be one-argument functions.
|
|
25
|
+
*/
|
|
26
|
+
class McpCorrelation {
|
|
27
|
+
jsonRpcId;
|
|
28
|
+
toolName;
|
|
29
|
+
// webpieces-disable no-any-unknown -- ContextKey value types are per-key; this one is context-only
|
|
30
|
+
static KEY = core_util_1.ContextKey.untrusted('webpieces-mcp-correlation', undefined, false,
|
|
31
|
+
/*isLogged*/ false);
|
|
32
|
+
constructor(jsonRpcId, toolName) {
|
|
33
|
+
this.jsonRpcId = jsonRpcId;
|
|
34
|
+
this.toolName = toolName;
|
|
35
|
+
}
|
|
36
|
+
/** Stamp what this request knows so far; `WpMcpServer` re-stamps once the tool name is known. */
|
|
37
|
+
// webpieces-disable no-function-outside-class -- static accessor trio for one context key
|
|
38
|
+
static stamp(correlation) {
|
|
39
|
+
core_context_1.RequestContext.putUntrusted(McpCorrelation.KEY, correlation);
|
|
40
|
+
}
|
|
41
|
+
/** A renderer reached before anything stamped (a failure with no parseable body) still renders. */
|
|
42
|
+
// webpieces-disable no-function-outside-class -- static accessor trio for one context key
|
|
43
|
+
static current() {
|
|
44
|
+
return core_context_1.RequestContext.getUntrusted(McpCorrelation.KEY) ?? new McpCorrelation(null);
|
|
45
|
+
}
|
|
46
|
+
/** The webpieces requestId of the request being served, as every MCP reply reports it. */
|
|
47
|
+
// webpieces-disable no-function-outside-class -- static accessor trio for one context key
|
|
48
|
+
static requestId() {
|
|
49
|
+
return core_context_1.RequestContext.getUntrusted(core_util_1.WebpiecesCoreHeaders.REQUEST_ID) ?? 'missing-request-id';
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.McpCorrelation = McpCorrelation;
|
|
53
|
+
/** The only error shape rendered into model-visible MCP tool content. */
|
|
54
|
+
class ModelVisibleToolError {
|
|
55
|
+
kind;
|
|
56
|
+
message;
|
|
57
|
+
requestId;
|
|
58
|
+
field;
|
|
59
|
+
callerMessage;
|
|
60
|
+
errorCode;
|
|
61
|
+
retryAfterSeconds;
|
|
62
|
+
statusCode;
|
|
63
|
+
constructor(kind, message, requestId, field, callerMessage, errorCode, retryAfterSeconds, statusCode) {
|
|
64
|
+
this.kind = kind;
|
|
65
|
+
this.message = message;
|
|
66
|
+
this.requestId = requestId;
|
|
67
|
+
this.field = field;
|
|
68
|
+
this.callerMessage = callerMessage;
|
|
69
|
+
this.errorCode = errorCode;
|
|
70
|
+
this.retryAfterSeconds = retryAfterSeconds;
|
|
71
|
+
this.statusCode = statusCode;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
exports.ModelVisibleToolError = ModelVisibleToolError;
|
|
75
|
+
/** JSON-RPC error body written by the HTTP boundary before the SDK is involved. */
|
|
76
|
+
class McpHttpErrorBody {
|
|
77
|
+
error;
|
|
78
|
+
id;
|
|
79
|
+
jsonrpc = '2.0';
|
|
80
|
+
constructor(error, id) {
|
|
81
|
+
this.error = error;
|
|
82
|
+
this.id = id;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
exports.McpHttpErrorBody = McpHttpErrorBody;
|
|
86
|
+
class McpHttpErrorDetail {
|
|
87
|
+
code;
|
|
88
|
+
message;
|
|
89
|
+
data;
|
|
90
|
+
constructor(code, message, data) {
|
|
91
|
+
this.code = code;
|
|
92
|
+
this.message = message;
|
|
93
|
+
this.data = data;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
exports.McpHttpErrorDetail = McpHttpErrorDetail;
|
|
97
|
+
/**
|
|
98
|
+
* webpieces' DEFAULT `tools/call` rendering, and what {@link McpRegistry} holds until an app installs
|
|
99
|
+
* its own. Public so an app's own {@link McpErrorTranslator} can DECLINE an error by delegating to it
|
|
100
|
+
* — the MCP twin of `WebpiecesDefaultErrorTranslator.toWire` on the HTTP side.
|
|
101
|
+
*
|
|
102
|
+
* Published text comes only from {@link ApiErrorBoundary.encode}: this never puts an error's own
|
|
103
|
+
* message in front of a model unless it is an `ApiEndUserError`.
|
|
104
|
+
*/
|
|
105
|
+
class McpDefaultToolCallRenderer {
|
|
106
|
+
boundary = new core_util_1.ApiErrorBoundary();
|
|
107
|
+
toWire(error) {
|
|
108
|
+
const requestId = McpCorrelation.requestId();
|
|
109
|
+
const visible = this.modelVisible(this.boundary.encode(error), requestId);
|
|
110
|
+
const text = JSON.stringify(visible);
|
|
111
|
+
return {
|
|
112
|
+
content: [{ type: 'text', text }],
|
|
113
|
+
isError: true,
|
|
114
|
+
_meta: { [exports.MCP_REQUEST_ID_META_KEY]: requestId },
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
modelVisible(payload, requestId) {
|
|
118
|
+
switch (payload.kind) {
|
|
119
|
+
case 'end-user':
|
|
120
|
+
return new ModelVisibleToolError(payload.kind, payload.message, requestId, undefined, undefined, payload.errorCode);
|
|
121
|
+
case 'bad-request':
|
|
122
|
+
return new ModelVisibleToolError(payload.kind, payload.callerMessage ?? payload.message, requestId, payload.field, payload.callerMessage);
|
|
123
|
+
case 'coded':
|
|
124
|
+
return new ModelVisibleToolError(payload.kind, payload.message, requestId, undefined, undefined, payload.errorCode, undefined, payload.statusCode);
|
|
125
|
+
case 'implementation':
|
|
126
|
+
return new ModelVisibleToolError(payload.kind, `Internal error in tool ${McpCorrelation.current().toolName ?? 'unknown'} ` +
|
|
127
|
+
`(requestId ${requestId}). This is a bug in the tool, not in your ` +
|
|
128
|
+
'arguments; retrying with different arguments will not help.', requestId);
|
|
129
|
+
default:
|
|
130
|
+
return new ModelVisibleToolError(payload.kind, payload.message, requestId, undefined, undefined, undefined, payload.retryAfterSeconds);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
exports.McpDefaultToolCallRenderer = McpDefaultToolCallRenderer;
|
|
135
|
+
//# sourceMappingURL=McpToolCallRendering.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"McpToolCallRendering.js","sourceRoot":"","sources":["../../../../../packages/http/mcp-server/src/McpToolCallRendering.ts"],"names":[],"mappings":";;;AACA,oDAM8B;AAC9B,0DAAyD;AAEzD,mGAAmG;A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ACX,OAAO,IAAI,qBAAqB,CAC5B,OAAO,CAAC,IAAI,EACZ,OAAO,CAAC,OAAO,EACf,SAAS,EACT,SAAS,EACT,SAAS,EACT,OAAO,CAAC,SAAS,CACpB,CAAC;YACN,KAAK,aAAa;gBACd,OAAO,IAAI,qBAAqB,CAC5B,OAAO,CAAC,IAAI,EACZ,OAAO,CAAC,aAAa,IAAI,OAAO,CAAC,OAAO,EACxC,SAAS,EACT,OAAO,CAAC,KAAK,EACb,OAAO,CAAC,aAAa,CACxB,CAAC;YACN,KAAK,OAAO;gBACR,OAAO,IAAI,qBAAqB,CAC5B,OAAO,CAAC,IAAI,EACZ,OAAO,CAAC,OAAO,EACf,SAAS,EACT,SAAS,EACT,SAAS,EACT,OAAO,CAAC,SAAS,EACjB,SAAS,EACT,OAAO,CAAC,UAAU,CACrB,CAAC;YACN,KAAK,gBAAgB;gBACjB,OAAO,IAAI,qBAAqB,CAC5B,OAAO,CAAC,IAAI,EACZ,0BAA0B,cAAc,CAAC,OAAO,EAAE,CAAC,QAAQ,IAAI,SAAS,GAAG;oBACvE,cAAc,SAAS,4CAA4C;oBACnE,6DAA6D,EACjE,SAAS,CACZ,CAAC;YACN;gBACI,OAAO,IAAI,qBAAqB,CAC5B,OAAO,CAAC,IAAI,EACZ,OAAO,CAAC,OAAO,EACf,SAAS,EACT,SAAS,EACT,SAAS,EACT,SAAS,EACT,OAAO,CAAC,iBAAiB,CAC5B,CAAC;QACV,CAAC;IACL,CAAC;CACJ;AAhED,gEAgEC","sourcesContent":["import { CallToolResult } from '@modelcontextprotocol/server';\nimport {\n ApiErrorBoundary,\n ApiErrorPayload,\n ContextKey,\n DtoValue,\n WebpiecesCoreHeaders,\n} from '@webpieces/core-util';\nimport { RequestContext } from '@webpieces/core-context';\n\n/** `_meta` key carrying the Webpieces requestId on every tools/call result, success or failure. */\nexport const MCP_REQUEST_ID_META_KEY = 'webpieces/requestId';\n\n/** JSON-RPC error `data` so a user can quote the requestId of any protocol-level failure. */\nexport class McpErrorData {\n constructor(public readonly requestId: string) {}\n}\n\n/**\n * The two per-request facts a renderer needs that are NOT the error: the JSON-RPC `id` it must echo,\n * and the tool being called. They are stamped into the request scope by `WpMcpServer` rather than\n * threaded through every renderer signature, because `requestId` already travels that way\n * ({@link WebpiecesCoreHeaders.REQUEST_ID}) and a second, parallel correlation parameter was a COPY\n * of the context — which is what the deleted `McpFailureScope` was.\n *\n * Context-only and never logged: the logging backends read `RequestContext` for correlation on every\n * record already, and this object exists purely so the renderers can be one-argument functions.\n */\nexport class McpCorrelation {\n // webpieces-disable no-any-unknown -- ContextKey value types are per-key; this one is context-only\n static readonly KEY = ContextKey.untrusted<McpCorrelation>(\n 'webpieces-mcp-correlation',\n undefined,\n false,\n /*isLogged*/ false,\n );\n\n constructor(\n public readonly jsonRpcId: string | number | null,\n public readonly toolName?: string,\n ) {}\n\n /** Stamp what this request knows so far; `WpMcpServer` re-stamps once the tool name is known. */\n // webpieces-disable no-function-outside-class -- static accessor trio for one context key\n static stamp(correlation: McpCorrelation): void {\n RequestContext.putUntrusted(McpCorrelation.KEY, correlation);\n }\n\n /** A renderer reached before anything stamped (a failure with no parseable body) still renders. */\n // webpieces-disable no-function-outside-class -- static accessor trio for one context key\n static current(): McpCorrelation {\n return RequestContext.getUntrusted(McpCorrelation.KEY) ?? new McpCorrelation(null);\n }\n\n /** The webpieces requestId of the request being served, as every MCP reply reports it. */\n // webpieces-disable no-function-outside-class -- static accessor trio for one context key\n static requestId(): string {\n return RequestContext.getUntrusted(WebpiecesCoreHeaders.REQUEST_ID) ?? 'missing-request-id';\n }\n}\n\n/** The only error shape rendered into model-visible MCP tool content. */\nexport class ModelVisibleToolError {\n constructor(\n public readonly kind: string,\n public readonly message: string,\n public readonly requestId: string,\n public readonly field?: string,\n public readonly callerMessage?: string,\n public readonly errorCode?: string,\n public readonly retryAfterSeconds?: number,\n public readonly statusCode?: number,\n ) {}\n}\n\n/** JSON-RPC error body written by the HTTP boundary before the SDK is involved. */\nexport class McpHttpErrorBody {\n readonly jsonrpc = '2.0';\n constructor(\n public readonly error: McpHttpErrorDetail,\n public readonly id: string | number | null,\n ) {}\n}\n\nexport class McpHttpErrorDetail {\n constructor(\n public readonly code: number,\n public readonly message: string,\n public readonly data: McpErrorData,\n ) {}\n}\n\n/**\n * The application's own `tools/call` error translation — the MCP twin of the `ErrorTranslator` an app\n * registers on `ClientRegistry` for HTTP and the `IpcErrorTranslator` it registers on `IpcRegistry`.\n * Register one with {@link McpRegistry.setErrorTranslator}.\n *\n * An app owns its error taxonomy and owns how those errors should be explained to a model, so a\n * claimed error yields the ENTIRE `CallToolResult` — content, `structuredContent`, `isError`, the lot.\n *\n * # There is NO `fromWire`, and that is the protocol, not an oversight\n *\n * The HTTP and IPC translators have two halves because webpieces sits on BOTH ends of those wires: it\n * writes the response AND, in another process, reads one. **webpieces is never the MCP client.** The\n * client is Claude (or another model host); there is no return path for webpieces to translate, so\n * the pair is one-directional by nature. Do not \"restore the symmetry\" by adding a half with nothing\n * to call it.\n *\n * # The pre-SDK boundary does NOT share the HTTP `ErrorTranslator`, despite the identical type\n *\n * `WpMcpErrorTranslator.toBearerBoundaryResponse` returns an `HttpResponseDto`, exactly like\n * `ErrorTranslator.toWire` — and it must STILL not be routed through it. Two reasons, both hard:\n *\n * - its body must stay JSON-RPC shaped (`McpHttpErrorBody`) or an MCP client cannot parse the reply\n * at all, whereas an app's HTTP translator publishes the app's own REST envelope;\n * - it emits the `401 + WWW-Authenticate: Bearer resource_metadata=...` that MCP clients use for OAuth\n * discovery, so an app rewriting it breaks connector onboarding.\n *\n * Same TYPE, different OWNER. `tools/list` is framework-owned for the same reason.\n *\n * Contract:\n * - `error` is the RAW thrown value: an app must be able to `instanceof` its own error classes.\n * `ApiErrorBoundary` never substitutes an object for the thrown error, so there is exactly one\n * error value on this path.\n * - There is no \"not mine\" return. A registered translator REPLACES the webpieces default and\n * declines by DELEGATING to it: `new McpDefaultToolCallRenderer().toWire(error)`.\n * - webpieces default-fills `_meta['webpieces/requestId']` only when the returned result has NO\n * `_meta`. An app that sets `_meta` owns it untouched.\n * - scope is `tools/call` ONLY.\n */\nexport interface McpErrorTranslator {\n /** An error in, the whole `tools/call` reply out. Named for its HTTP and IPC twins. */\n toWire(error: Error): CallToolResult;\n}\n\n/**\n * webpieces' DEFAULT `tools/call` rendering, and what {@link McpRegistry} holds until an app installs\n * its own. Public so an app's own {@link McpErrorTranslator} can DECLINE an error by delegating to it\n * — the MCP twin of `WebpiecesDefaultErrorTranslator.toWire` on the HTTP side.\n *\n * Published text comes only from {@link ApiErrorBoundary.encode}: this never puts an error's own\n * message in front of a model unless it is an `ApiEndUserError`.\n */\nexport class McpDefaultToolCallRenderer implements McpErrorTranslator {\n private readonly boundary = new ApiErrorBoundary();\n\n toWire(error: Error): CallToolResult {\n const requestId = McpCorrelation.requestId();\n const visible = this.modelVisible(this.boundary.encode(error), requestId);\n const text = JSON.stringify(visible as DtoValue);\n return {\n content: [{ type: 'text', text }],\n isError: true,\n _meta: { [MCP_REQUEST_ID_META_KEY]: requestId },\n };\n }\n\n private modelVisible(payload: ApiErrorPayload, requestId: string): ModelVisibleToolError {\n switch (payload.kind) {\n case 'end-user':\n return new ModelVisibleToolError(\n payload.kind,\n payload.message,\n requestId,\n undefined,\n undefined,\n payload.errorCode,\n );\n case 'bad-request':\n return new ModelVisibleToolError(\n payload.kind,\n payload.callerMessage ?? payload.message,\n requestId,\n payload.field,\n payload.callerMessage,\n );\n case 'coded':\n return new ModelVisibleToolError(\n payload.kind,\n payload.message,\n requestId,\n undefined,\n undefined,\n payload.errorCode,\n undefined,\n payload.statusCode,\n );\n case 'implementation':\n return new ModelVisibleToolError(\n payload.kind,\n `Internal error in tool ${McpCorrelation.current().toolName ?? 'unknown'} ` +\n `(requestId ${requestId}). This is a bug in the tool, not in your ` +\n 'arguments; retrying with different arguments will not help.',\n requestId,\n );\n default:\n return new ModelVisibleToolError(\n payload.kind,\n payload.message,\n requestId,\n undefined,\n undefined,\n undefined,\n payload.retryAfterSeconds,\n );\n }\n }\n}\n"]}
|
|
@@ -1,98 +1,8 @@
|
|
|
1
1
|
import { CallToolResult, ProtocolError } from '@modelcontextprotocol/server';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
export declare const MCP_REQUEST_ID_META_KEY = "webpieces/requestId";
|
|
5
|
-
/** JSON-RPC error `data` so a user can quote the requestId of any protocol-level failure. */
|
|
6
|
-
export declare class McpErrorData {
|
|
7
|
-
readonly requestId: string;
|
|
8
|
-
constructor(requestId: string);
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* The two per-request facts a renderer needs that are NOT the error: the JSON-RPC `id` it must echo,
|
|
12
|
-
* and the tool being called. They are stamped into the request scope by `WpMcpServer` rather than
|
|
13
|
-
* threaded through every renderer signature, because `requestId` already travels that way
|
|
14
|
-
* ({@link WebpiecesCoreHeaders.REQUEST_ID}) and a second, parallel correlation parameter was a COPY
|
|
15
|
-
* of the context — which is what the deleted `McpFailureScope` was.
|
|
16
|
-
*
|
|
17
|
-
* Context-only and never logged: the logging backends read `RequestContext` for correlation on every
|
|
18
|
-
* record already, and this object exists purely so the renderers can be one-argument functions.
|
|
19
|
-
*/
|
|
20
|
-
export declare class McpCorrelation {
|
|
21
|
-
readonly jsonRpcId: string | number | null;
|
|
22
|
-
readonly toolName?: string | undefined;
|
|
23
|
-
static readonly KEY: ContextKey<McpCorrelation, "untrusted">;
|
|
24
|
-
constructor(jsonRpcId: string | number | null, toolName?: string | undefined);
|
|
25
|
-
/** Stamp what this request knows so far; `WpMcpServer` re-stamps once the tool name is known. */
|
|
26
|
-
static stamp(correlation: McpCorrelation): void;
|
|
27
|
-
/** A renderer reached before anything stamped (a failure with no parseable body) still renders. */
|
|
28
|
-
static current(): McpCorrelation;
|
|
29
|
-
/** The webpieces requestId of the request being served, as every MCP reply reports it. */
|
|
30
|
-
static requestId(): string;
|
|
31
|
-
}
|
|
32
|
-
/** The only error shape rendered into model-visible MCP tool content. */
|
|
33
|
-
export declare class ModelVisibleToolError {
|
|
34
|
-
readonly kind: string;
|
|
35
|
-
readonly message: string;
|
|
36
|
-
readonly requestId: string;
|
|
37
|
-
readonly field?: string | undefined;
|
|
38
|
-
readonly callerMessage?: string | undefined;
|
|
39
|
-
readonly errorCode?: string | undefined;
|
|
40
|
-
readonly retryAfterSeconds?: number | undefined;
|
|
41
|
-
readonly statusCode?: number | undefined;
|
|
42
|
-
constructor(kind: string, message: string, requestId: string, field?: string | undefined, callerMessage?: string | undefined, errorCode?: string | undefined, retryAfterSeconds?: number | undefined, statusCode?: number | undefined);
|
|
43
|
-
}
|
|
44
|
-
/** JSON-RPC error body written by the HTTP boundary before the SDK is involved. */
|
|
45
|
-
export declare class McpHttpErrorBody {
|
|
46
|
-
readonly error: McpHttpErrorDetail;
|
|
47
|
-
readonly id: string | number | null;
|
|
48
|
-
readonly jsonrpc = "2.0";
|
|
49
|
-
constructor(error: McpHttpErrorDetail, id: string | number | null);
|
|
50
|
-
}
|
|
51
|
-
export declare class McpHttpErrorDetail {
|
|
52
|
-
readonly code: number;
|
|
53
|
-
readonly message: string;
|
|
54
|
-
readonly data: McpErrorData;
|
|
55
|
-
constructor(code: number, message: string, data: McpErrorData);
|
|
56
|
-
}
|
|
2
|
+
import { HttpResponseDto } from '@webpieces/core-util';
|
|
3
|
+
import { McpHttpErrorBody } from './McpToolCallRendering';
|
|
57
4
|
/**
|
|
58
|
-
* The
|
|
59
|
-
* app registers on `ClientRegistry` for the HTTP path. Register one with
|
|
60
|
-
* `WpMcpServerConfig.setErrorTranslator(...)`; it is NOT a global, because `WpMcpServer` is
|
|
61
|
-
* constructed by app code and two servers may run in one process.
|
|
62
|
-
*
|
|
63
|
-
* An app owns its error taxonomy and owns how those errors should be explained to a model, so a
|
|
64
|
-
* claimed error yields the ENTIRE `CallToolResult` — content, `structuredContent`, `isError`, the lot.
|
|
65
|
-
*
|
|
66
|
-
* Contract:
|
|
67
|
-
* - `error` is the RAW thrown value: an app must be able to `instanceof` its own error classes.
|
|
68
|
-
* `ApiErrorBoundary` never substitutes an object for the thrown error, so there is exactly one
|
|
69
|
-
* error value on this path.
|
|
70
|
-
* - There is no "not mine" return. A registered translator REPLACES the webpieces default and
|
|
71
|
-
* declines by DELEGATING to it: `new McpDefaultToolCallRenderer().toToolCallResult(error)`.
|
|
72
|
-
* - webpieces default-fills `_meta['webpieces/requestId']` only when the returned result has NO
|
|
73
|
-
* `_meta`. An app that sets `_meta` owns it untouched.
|
|
74
|
-
* - scope is `tools/call` ONLY. The pre-SDK HTTP boundary and `tools/list` stay framework-owned:
|
|
75
|
-
* that boundary emits the `401 + WWW-Authenticate: Bearer resource_metadata=...` MCP clients
|
|
76
|
-
* depend on for OAuth discovery, and an app rewriting it breaks connector onboarding.
|
|
77
|
-
*/
|
|
78
|
-
export interface McpErrorTranslators {
|
|
79
|
-
toToolCallResult(error: Error): CallToolResult;
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* webpieces' DEFAULT `tools/call` rendering, as a public class so an app's own
|
|
83
|
-
* {@link McpErrorTranslators} can DECLINE an error by delegating to it — the MCP twin of
|
|
84
|
-
* `ApiErrorHttpMapper.toResponse` on the HTTP side.
|
|
85
|
-
*
|
|
86
|
-
* Published text comes only from {@link ApiErrorBoundary.encode}: this never puts an error's own
|
|
87
|
-
* message in front of a model unless it is an `ApiEndUserError`.
|
|
88
|
-
*/
|
|
89
|
-
export declare class McpDefaultToolCallRenderer implements McpErrorTranslators {
|
|
90
|
-
private readonly boundary;
|
|
91
|
-
toToolCallResult(error: Error): CallToolResult;
|
|
92
|
-
private modelVisible;
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* The ONE place every MCP failure becomes a reply, mirroring `ApiErrorHttpMapper` for HTTP: an error
|
|
5
|
+
* The ONE place every MCP failure becomes a reply, mirroring `WebpiecesDefaultErrorTranslator` for HTTP: an error
|
|
96
6
|
* in, the exact wire shape out. There is one method per BOUNDARY, and the MCP spec is what makes
|
|
97
7
|
* them three rather than one:
|
|
98
8
|
*
|
|
@@ -108,15 +18,13 @@ export declare class McpDefaultToolCallRenderer implements McpErrorTranslators {
|
|
|
108
18
|
*/
|
|
109
19
|
export declare class WpMcpErrorTranslator {
|
|
110
20
|
private readonly challenge;
|
|
111
|
-
private readonly appTranslators?;
|
|
112
21
|
private readonly boundary;
|
|
113
22
|
private readonly defaultToolCall;
|
|
114
23
|
/**
|
|
115
24
|
* @param challenge - the `WWW-Authenticate` value for a 401, read lazily because
|
|
116
25
|
* `WpMcpServerConfig` is only fully validated at `bind(...)` time.
|
|
117
|
-
* @param appTranslators - the app's `tools/call` seam; `undefined` means webpieces renders all.
|
|
118
26
|
*/
|
|
119
|
-
constructor(challenge: () => string
|
|
27
|
+
constructor(challenge: () => string);
|
|
120
28
|
/**
|
|
121
29
|
* Boundary #1: everything webpieces itself rejects before the SDK is involved — bearer
|
|
122
30
|
* verification, `Origin`, a malformed body. This is 401/403/400/500 today and any status
|
|
@@ -137,15 +45,16 @@ export declare class WpMcpErrorTranslator {
|
|
|
137
45
|
* error's operator text to the caller.
|
|
138
46
|
*/
|
|
139
47
|
toListError(error: Error): never;
|
|
140
|
-
/** tools/call with a name no binding registered: a JSON-RPC protocol error
|
|
48
|
+
/** tools/call with a name no binding registered: a JSON-RPC protocol error in either MCP era. */
|
|
141
49
|
unknownTool(toolName: string): ProtocolError;
|
|
142
50
|
/**
|
|
143
51
|
* Boundary #3: every tools/call failure after the tool is found — an `isError: true` result the
|
|
144
52
|
* model sees.
|
|
145
53
|
*
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
*
|
|
54
|
+
* ONE unconditional line: {@link McpRegistry.getErrorTranslator} is never undefined, so there is
|
|
55
|
+
* no "was one registered" branch. A registered {@link McpErrorTranslator} REPLACES the default and
|
|
56
|
+
* declines by delegating to {@link McpDefaultToolCallRenderer}, so "no translator registered" and
|
|
57
|
+
* "a translator that does not claim this error" produce byte-identical results.
|
|
149
58
|
*/
|
|
150
59
|
toToolCallResult(failure: Error): CallToolResult;
|
|
151
60
|
/** The `_meta` every tools/call result carries so a user can quote its requestId. */
|
|
@@ -154,6 +63,9 @@ export declare class WpMcpErrorTranslator {
|
|
|
154
63
|
* An app translator that THROWS must not replace the failure being reported: its own bug is
|
|
155
64
|
* logged here and the ORIGINAL error still renders through the webpieces default, so the model
|
|
156
65
|
* still gets a reply carrying the requestId.
|
|
66
|
+
*
|
|
67
|
+
* This is BUG CONTAINMENT, not a "did anyone register one" branch — the same shape as
|
|
68
|
+
* `ClientErrorTranslator.throwIfFailure` on the HTTP side.
|
|
157
69
|
*/
|
|
158
70
|
private appOrDefault;
|
|
159
71
|
/**
|