@lushly-dev/afd-server 2.4.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 +239 -56
- package/dist/admission.d.ts +137 -0
- package/dist/admission.d.ts.map +1 -0
- package/dist/admission.js +144 -0
- package/dist/admission.js.map +1 -0
- package/dist/app-call-registry.d.ts +31 -0
- package/dist/app-call-registry.d.ts.map +1 -0
- package/dist/app-call-registry.js +64 -0
- package/dist/app-call-registry.js.map +1 -0
- package/dist/approval-admission.d.ts +139 -0
- package/dist/approval-admission.d.ts.map +1 -0
- package/dist/approval-admission.js +252 -0
- package/dist/approval-admission.js.map +1 -0
- package/dist/approval-crypto.d.ts +37 -17
- package/dist/approval-crypto.d.ts.map +1 -1
- package/dist/approval-crypto.js +84 -75
- package/dist/approval-crypto.js.map +1 -1
- package/dist/approval-process-state.d.ts +21 -0
- package/dist/approval-process-state.d.ts.map +1 -0
- package/dist/approval-process-state.js +29 -0
- package/dist/approval-process-state.js.map +1 -0
- package/dist/approval-relay.d.ts +192 -9
- package/dist/approval-relay.d.ts.map +1 -1
- package/dist/approval-relay.js +45 -9
- package/dist/approval-relay.js.map +1 -1
- package/dist/approval-up-front.d.ts +40 -0
- package/dist/approval-up-front.d.ts.map +1 -0
- package/dist/approval-up-front.js +409 -0
- package/dist/approval-up-front.js.map +1 -0
- package/dist/approval.d.ts +65 -12
- package/dist/approval.d.ts.map +1 -1
- package/dist/approval.js +311 -47
- package/dist/approval.js.map +1 -1
- package/dist/approver.d.ts +84 -0
- package/dist/approver.d.ts.map +1 -0
- package/dist/approver.js +107 -0
- package/dist/approver.js.map +1 -0
- package/dist/auth-middleware-check.d.ts +23 -0
- package/dist/auth-middleware-check.d.ts.map +1 -0
- package/dist/auth-middleware-check.js +48 -0
- package/dist/auth-middleware-check.js.map +1 -0
- package/dist/bootstrap/afd-context.d.ts.map +1 -1
- package/dist/bootstrap/afd-context.js +27 -3
- package/dist/bootstrap/afd-context.js.map +1 -1
- package/dist/bootstrap/afd-docs.d.ts.map +1 -1
- package/dist/bootstrap/afd-docs.js +4 -0
- package/dist/bootstrap/afd-docs.js.map +1 -1
- package/dist/bootstrap/afd-help.d.ts +4 -1
- package/dist/bootstrap/afd-help.d.ts.map +1 -1
- package/dist/bootstrap/afd-help.js +19 -4
- package/dist/bootstrap/afd-help.js.map +1 -1
- package/dist/bootstrap/described-command.d.ts +3 -1
- package/dist/bootstrap/described-command.d.ts.map +1 -1
- package/dist/bootstrap/described-command.js.map +1 -1
- package/dist/command-allow.d.ts +4 -4
- package/dist/command-allow.d.ts.map +1 -1
- package/dist/command-allow.js +10 -15
- package/dist/command-allow.js.map +1 -1
- package/dist/command-routing.d.ts +56 -8
- package/dist/command-routing.d.ts.map +1 -1
- package/dist/command-routing.js +82 -16
- package/dist/command-routing.js.map +1 -1
- package/dist/command-timeout.d.ts +17 -0
- package/dist/command-timeout.d.ts.map +1 -0
- package/dist/command-timeout.js +99 -0
- package/dist/command-timeout.js.map +1 -0
- package/dist/confirmation-gate.d.ts +7 -7
- package/dist/confirmation-gate.d.ts.map +1 -1
- package/dist/confirmation-gate.js +26 -12
- package/dist/confirmation-gate.js.map +1 -1
- package/dist/confirmations.d.ts +28 -3
- package/dist/confirmations.d.ts.map +1 -1
- package/dist/confirmations.js +6 -7
- package/dist/confirmations.js.map +1 -1
- package/dist/context-fields.d.ts +16 -1
- package/dist/context-fields.d.ts.map +1 -1
- package/dist/context-fields.js +30 -13
- package/dist/context-fields.js.map +1 -1
- package/dist/context-scope.d.ts +3 -3
- package/dist/context-scope.d.ts.map +1 -1
- package/dist/context-scope.js +7 -8
- package/dist/context-scope.js.map +1 -1
- package/dist/define-core.d.ts +63 -1
- package/dist/define-core.d.ts.map +1 -1
- package/dist/define-core.js +27 -4
- package/dist/define-core.js.map +1 -1
- package/dist/define.d.ts +4 -3
- package/dist/define.d.ts.map +1 -1
- package/dist/define.js +1 -1
- package/dist/define.js.map +1 -1
- package/dist/direct-registry.d.ts +54 -8
- package/dist/direct-registry.d.ts.map +1 -1
- package/dist/direct-registry.js +64 -8
- package/dist/direct-registry.js.map +1 -1
- package/dist/direct-tool-surface.d.ts +1 -1
- package/dist/direct-tool-surface.d.ts.map +1 -1
- package/dist/direct-tool-surface.js +28 -6
- package/dist/direct-tool-surface.js.map +1 -1
- package/dist/direct.d.ts +6 -2
- package/dist/direct.d.ts.map +1 -1
- package/dist/direct.js +4 -1
- package/dist/direct.js.map +1 -1
- package/dist/elicitation-state.d.ts +5 -1
- package/dist/elicitation-state.d.ts.map +1 -1
- package/dist/elicitation-state.js +4 -1
- package/dist/elicitation-state.js.map +1 -1
- package/dist/execution.d.ts +52 -6
- package/dist/execution.d.ts.map +1 -1
- package/dist/execution.js +183 -19
- package/dist/execution.js.map +1 -1
- package/dist/host-state.d.ts +81 -0
- package/dist/host-state.d.ts.map +1 -0
- package/dist/host-state.js +62 -0
- package/dist/host-state.js.map +1 -0
- package/dist/http-handler.d.ts +48 -11
- package/dist/http-handler.d.ts.map +1 -1
- package/dist/http-handler.js +207 -58
- package/dist/http-handler.js.map +1 -1
- package/dist/http-security.d.ts +14 -1
- package/dist/http-security.d.ts.map +1 -1
- package/dist/http-security.js +79 -17
- package/dist/http-security.js.map +1 -1
- package/dist/http-sessions.d.ts +45 -5
- package/dist/http-sessions.d.ts.map +1 -1
- package/dist/http-sessions.js +52 -10
- package/dist/http-sessions.js.map +1 -1
- package/dist/http-sse.d.ts +9 -0
- package/dist/http-sse.d.ts.map +1 -1
- package/dist/http-sse.js +12 -3
- package/dist/http-sse.js.map +1 -1
- package/dist/human-relay.d.ts +195 -0
- package/dist/human-relay.d.ts.map +1 -0
- package/dist/human-relay.js +670 -0
- package/dist/human-relay.js.map +1 -0
- package/dist/idempotency-step.d.ts +231 -0
- package/dist/idempotency-step.d.ts.map +1 -0
- package/dist/idempotency-step.js +389 -0
- package/dist/idempotency-step.js.map +1 -0
- package/dist/idempotency-store.d.ts +107 -0
- package/dist/idempotency-store.d.ts.map +1 -0
- package/dist/idempotency-store.js +100 -0
- package/dist/idempotency-store.js.map +1 -0
- package/dist/index.d.ts +18 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -4
- package/dist/index.js.map +1 -1
- package/dist/instance-state.d.ts +53 -0
- package/dist/instance-state.d.ts.map +1 -0
- package/dist/instance-state.js +94 -0
- package/dist/instance-state.js.map +1 -0
- package/dist/journal-off-tape.d.ts +52 -0
- package/dist/journal-off-tape.d.ts.map +1 -0
- package/dist/journal-off-tape.js +150 -0
- package/dist/journal-off-tape.js.map +1 -0
- package/dist/journal-replay.d.ts.map +1 -1
- package/dist/journal-replay.js +48 -3
- package/dist/journal-replay.js.map +1 -1
- package/dist/journal-view.d.ts +50 -0
- package/dist/journal-view.d.ts.map +1 -0
- package/dist/journal-view.js +214 -0
- package/dist/journal-view.js.map +1 -0
- package/dist/journal.d.ts +34 -6
- package/dist/journal.d.ts.map +1 -1
- package/dist/journal.js +183 -47
- package/dist/journal.js.map +1 -1
- package/dist/json-rpc.d.ts +3 -2
- package/dist/json-rpc.d.ts.map +1 -1
- package/dist/json-rpc.js +5 -3
- package/dist/json-rpc.js.map +1 -1
- package/dist/lazy-tools.d.ts +27 -2
- package/dist/lazy-tools.d.ts.map +1 -1
- package/dist/lazy-tools.js +22 -4
- package/dist/lazy-tools.js.map +1 -1
- package/dist/mcp-auth-bearer.d.ts +2 -0
- package/dist/mcp-auth-bearer.d.ts.map +1 -1
- package/dist/mcp-auth-bearer.js +1 -1
- package/dist/mcp-auth-bearer.js.map +1 -1
- package/dist/mcp-auth-dispatch.d.ts +38 -0
- package/dist/mcp-auth-dispatch.d.ts.map +1 -0
- package/dist/mcp-auth-dispatch.js +113 -0
- package/dist/mcp-auth-dispatch.js.map +1 -0
- package/dist/mcp-auth-jwt.d.ts +3 -2
- package/dist/mcp-auth-jwt.d.ts.map +1 -1
- package/dist/mcp-auth-jwt.js.map +1 -1
- package/dist/mcp-auth-resource.d.ts +6 -2
- package/dist/mcp-auth-resource.d.ts.map +1 -1
- package/dist/mcp-auth-resource.js +6 -5
- package/dist/mcp-auth-resource.js.map +1 -1
- package/dist/mcp-auth-scopes.d.ts +84 -0
- package/dist/mcp-auth-scopes.d.ts.map +1 -0
- package/dist/mcp-auth-scopes.js +125 -0
- package/dist/mcp-auth-scopes.js.map +1 -0
- package/dist/mcp-auth-view.d.ts +50 -0
- package/dist/mcp-auth-view.d.ts.map +1 -0
- package/dist/mcp-auth-view.js +179 -0
- package/dist/mcp-auth-view.js.map +1 -0
- package/dist/mcp-auth.d.ts +94 -3
- package/dist/mcp-auth.d.ts.map +1 -1
- package/dist/mcp-auth.js +241 -23
- package/dist/mcp-auth.js.map +1 -1
- package/dist/mcp-modern.d.ts +80 -6
- package/dist/mcp-modern.d.ts.map +1 -1
- package/dist/mcp-modern.js +378 -91
- package/dist/mcp-modern.js.map +1 -1
- package/dist/meta-argument.d.ts +9 -8
- package/dist/meta-argument.d.ts.map +1 -1
- package/dist/meta-argument.js +28 -10
- package/dist/meta-argument.js.map +1 -1
- package/dist/meta-tools.js +2 -2
- package/dist/meta-tools.js.map +1 -1
- package/dist/middleware.d.ts +16 -1
- package/dist/middleware.d.ts.map +1 -1
- package/dist/middleware.js +57 -10
- package/dist/middleware.js.map +1 -1
- package/dist/output-budget.d.ts +77 -0
- package/dist/output-budget.d.ts.map +1 -0
- package/dist/output-budget.js +202 -0
- package/dist/output-budget.js.map +1 -0
- package/dist/principal-binding.js +1 -1
- package/dist/principal-binding.js.map +1 -1
- package/dist/relay-evidence.d.ts +74 -0
- package/dist/relay-evidence.d.ts.map +1 -0
- package/dist/relay-evidence.js +143 -0
- package/dist/relay-evidence.js.map +1 -0
- package/dist/resources.d.ts +35 -10
- package/dist/resources.d.ts.map +1 -1
- package/dist/resources.js +59 -18
- package/dist/resources.js.map +1 -1
- package/dist/schema.d.ts +4 -3
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +3 -3
- package/dist/schema.js.map +1 -1
- package/dist/scope-view.d.ts +65 -0
- package/dist/scope-view.d.ts.map +1 -0
- package/dist/scope-view.js +45 -0
- package/dist/scope-view.js.map +1 -0
- package/dist/server-types.d.ts +182 -27
- package/dist/server-types.d.ts.map +1 -1
- package/dist/server-types.js.map +1 -1
- package/dist/server.d.ts +26 -0
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +403 -61
- package/dist/server.js.map +1 -1
- package/dist/skills.d.ts.map +1 -1
- package/dist/skills.js +99 -3
- package/dist/skills.js.map +1 -1
- package/dist/task-dispatch.d.ts +35 -0
- package/dist/task-dispatch.d.ts.map +1 -0
- package/dist/task-dispatch.js +25 -0
- package/dist/task-dispatch.js.map +1 -0
- package/dist/tasks-runtime.d.ts +115 -0
- package/dist/tasks-runtime.d.ts.map +1 -0
- package/dist/tasks-runtime.js +760 -0
- package/dist/tasks-runtime.js.map +1 -0
- package/dist/tasks.d.ts +237 -0
- package/dist/tasks.d.ts.map +1 -0
- package/dist/tasks.js +321 -0
- package/dist/tasks.js.map +1 -0
- package/dist/tool-router.d.ts +68 -3
- package/dist/tool-router.d.ts.map +1 -1
- package/dist/tool-router.js +293 -70
- package/dist/tool-router.js.map +1 -1
- package/dist/tools.d.ts +32 -0
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +162 -55
- package/dist/tools.js.map +1 -1
- package/dist/up-front-steps.d.ts +35 -0
- package/dist/up-front-steps.d.ts.map +1 -0
- package/dist/up-front-steps.js +159 -0
- package/dist/up-front-steps.js.map +1 -0
- package/dist/validation.d.ts +38 -3
- package/dist/validation.d.ts.map +1 -1
- package/dist/validation.js +118 -23
- package/dist/validation.js.map +1 -1
- package/dist/verified-credentials.d.ts +13 -5
- package/dist/verified-credentials.d.ts.map +1 -1
- package/dist/verified-credentials.js +43 -10
- package/dist/verified-credentials.js.map +1 -1
- package/package.json +15 -11
package/dist/server.js
CHANGED
|
@@ -5,36 +5,99 @@
|
|
|
5
5
|
* execution, tool routing, and transport layers into a single MCP server.
|
|
6
6
|
*/
|
|
7
7
|
import { createServer } from 'node:http';
|
|
8
|
-
import { isMcpExposed } from '@lushly-dev/afd-core';
|
|
8
|
+
import { executePipeline as executeCorePipeline, failure, isMcpExposed, } from '@lushly-dev/afd-core';
|
|
9
|
+
import { validateOutputBudget } from '@lushly-dev/afd-core/commands';
|
|
10
|
+
import { callNotGranted } from '@lushly-dev/afd-core/internal/app-calls';
|
|
11
|
+
import { approvalProcessState } from './approval-process-state.js';
|
|
12
|
+
import { upFrontApproval } from './approval-relay.js';
|
|
13
|
+
import { assertAuthMiddlewareRecognisesTokens } from './auth-middleware-check.js';
|
|
9
14
|
import { createContextCommands, getBootstrapCommands } from './bootstrap/registry.js';
|
|
10
15
|
import { assertValidCommandNames, assertValidFirstClassTools } from './command-names.js';
|
|
11
16
|
import { filterByContext } from './command-routing.js';
|
|
17
|
+
import { resolveDefaultTimeoutMs, withCommandDeadlines } from './command-timeout.js';
|
|
12
18
|
import { createConfirmationGate } from './confirmation-gate.js';
|
|
13
19
|
import { checkContextFields } from './context-fields.js';
|
|
14
20
|
import { createContextState, resolveContextState } from './context-scope.js';
|
|
15
21
|
import { createExecutionEngine } from './execution.js';
|
|
16
|
-
import { createHttpHandler } from './http-handler.js';
|
|
17
|
-
import { createSessionStore } from './http-sessions.js';
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
22
|
+
import { checkHttpHandlerOptions, createHttpHandler } from './http-handler.js';
|
|
23
|
+
import { createSessionStore, DEFAULT_MAX_SESSIONS, sessionLimits, sessionStoreLimits, } from './http-sessions.js';
|
|
24
|
+
import { createHumanRelayGate } from './human-relay.js';
|
|
25
|
+
import { createIdempotencyStep } from './idempotency-step.js';
|
|
26
|
+
import { InMemoryIdempotencyStore } from './idempotency-store.js';
|
|
27
|
+
import { checkInstanceState, warnInstancesOnce } from './instance-state.js';
|
|
28
|
+
import { isJournalMiddleware } from './journal.js';
|
|
29
|
+
import { checkAuthOptions, createAuthLayer, } from './mcp-auth.js';
|
|
30
|
+
import { checkModernBodyLimit, createModernHttpLane, DEFAULT_TOOLS_LIST_TTL_MS, defaultInstructions, resolveToolsListTtlMs, serveModernStdio, serverContractFor, } from './mcp-modern.js';
|
|
20
31
|
import { assertToolMeta } from './meta-keys.js';
|
|
32
|
+
import { clearRelayEvidence, takeRelayDispatchAdmission, takeRelayValidatedInput, transferRelayState, } from './relay-evidence.js';
|
|
21
33
|
import { createResourceService } from './resources.js';
|
|
34
|
+
import { listedCommands, scopeViewOf, shownOnly } from './scope-view.js';
|
|
22
35
|
import { isStdinPiped } from './server-types.js';
|
|
36
|
+
import { assertValidTaskCommands } from './tasks.js';
|
|
37
|
+
import { TaskRuntime } from './tasks-runtime.js';
|
|
23
38
|
import { createToolRouter } from './tool-router.js';
|
|
24
|
-
import {
|
|
39
|
+
import { createToolsList } from './tools.js';
|
|
40
|
+
import { checkApprovedAtRun } from './up-front-steps.js';
|
|
41
|
+
import { resolveMaxValidationIssues, validateInputEnhanced } from './validation.js';
|
|
25
42
|
// Re-export public types so existing imports from './server.js' still work
|
|
26
43
|
export { isStdinPiped, MAX_FIRST_CLASS_TOOLS } from './server-types.js';
|
|
27
44
|
// ═══════════════════════════════════════════════════════════════════════════════
|
|
28
45
|
// SERVER FACTORY
|
|
29
46
|
// ═══════════════════════════════════════════════════════════════════════════════
|
|
30
|
-
|
|
31
|
-
|
|
47
|
+
/**
|
|
48
|
+
* M6, "At execution": with a resolver, every command a call with credentials runs is decided again
|
|
49
|
+
* on its validated input (defaults applied, pipeline references resolved), after the required
|
|
50
|
+
* scopes. First in the remote engine's chain, so a missing scope reaches no other middleware.
|
|
51
|
+
*/
|
|
52
|
+
const executionScopes = async (commandName, input, context, next) => {
|
|
53
|
+
const view = scopeViewOf(context);
|
|
54
|
+
const decision = view?.decideExecution(commandName, input);
|
|
55
|
+
return view && decision && decision !== 'proceed'
|
|
56
|
+
? failure(view.insufficientScope(decision.forbidden))
|
|
57
|
+
: next();
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* The wiring every lane of one server shares, stdio's included: engines, routers, the tools list
|
|
61
|
+
* and the MCP lanes' dependencies. It builds nothing HTTP-only (#1746): that is
|
|
62
|
+
* {@link createHttpRuntime}'s, which also calls `httpRouting` for the HTTP routes' engine and
|
|
63
|
+
* router. Every option either one reads is checked here, so a server that would fail over HTTP
|
|
64
|
+
* fails to build over stdio too.
|
|
65
|
+
*/
|
|
66
|
+
function createServerCore(options) {
|
|
67
|
+
const { name, version, commands, port = 3100, host = 'localhost', devMode = false, middleware = [], onCommand, onError, toolStrategy = 'grouped', groupByFn, contexts, bootstrap = false, } = options;
|
|
68
|
+
// Checked first: a server that would serve every token's call as the local session's user does
|
|
69
|
+
// not start (afd-auth's middleware without `isVerifiedCredentials` beside `auth`), nor one that
|
|
70
|
+
// would depend on one process when told it has several (`instances`).
|
|
71
|
+
assertAuthMiddlewareRecognisesTokens(options.auth !== undefined, options.middleware);
|
|
72
|
+
const instanceState = checkInstanceState({
|
|
73
|
+
name,
|
|
74
|
+
instances: options.instances,
|
|
75
|
+
confirmationKey: options.confirmationKey,
|
|
76
|
+
confirmations: options.confirmations,
|
|
77
|
+
hasHumanRelay: Boolean(options.humanRelay),
|
|
78
|
+
middleware,
|
|
79
|
+
// The default store, or one given explicitly, keeps keys in this process only (S13).
|
|
80
|
+
perProcessIdempotency: options.idempotency !== undefined &&
|
|
81
|
+
(options.idempotency.store === undefined ||
|
|
82
|
+
options.idempotency.store instanceof InMemoryIdempotencyStore),
|
|
83
|
+
});
|
|
32
84
|
// A copy: the set must not change while the server runs.
|
|
33
85
|
const firstClassTools = options.firstClassTools ? [...options.firstClassTools] : undefined;
|
|
34
86
|
assertValidCommandNames(commands, { bootstrap, contexts: Boolean(contexts?.length) });
|
|
35
|
-
// A command built by hand skips `defineCommand`'s check; registration refuses a bad key
|
|
36
|
-
|
|
87
|
+
// A command built by hand skips `defineCommand`'s check; registration refuses a bad key or budget
|
|
88
|
+
// and snapshots the validated budget so later mutation of the input object cannot alter it.
|
|
89
|
+
const validatedCommands = commands.map((command) => {
|
|
37
90
|
assertToolMeta(command.name, command.toolMeta);
|
|
91
|
+
if (command.idempotency !== undefined &&
|
|
92
|
+
command.idempotency !== 'natural' &&
|
|
93
|
+
command.idempotency !== 'keyed') {
|
|
94
|
+
throw new Error(`command "${command.name}": idempotency must be 'natural' or 'keyed' (spec/idempotency.md, D1)`);
|
|
95
|
+
}
|
|
96
|
+
return command.outputBudget !== undefined
|
|
97
|
+
? { ...command, outputBudget: validateOutputBudget(command.outputBudget, command.name) }
|
|
98
|
+
: command;
|
|
99
|
+
});
|
|
100
|
+
assertValidTaskCommands(validatedCommands);
|
|
38
101
|
// Resource entries and the skill catalog are checked now, so a bad one stops the server from
|
|
39
102
|
// being built. Their results do not vary by context, so contexts leave the TTL alone.
|
|
40
103
|
const resourcesTtlMs = options.toolsListTtlMs ?? DEFAULT_TOOLS_LIST_TTL_MS;
|
|
@@ -43,14 +106,18 @@ function createSharedHttpRuntime(options) {
|
|
|
43
106
|
// state (or none), bound to their command context by the HTTP handler.
|
|
44
107
|
const contextState = contexts?.length ? createContextState() : undefined;
|
|
45
108
|
const { clock } = options;
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
109
|
+
if (options.maxSessionsPerOwner !== undefined) {
|
|
110
|
+
if (!options.auth) {
|
|
111
|
+
throw new Error('maxSessionsPerOwner needs auth: without it every HTTP caller is one owner, whose quota is maxSessions');
|
|
112
|
+
}
|
|
113
|
+
// Checked even without `contexts`, where no session is issued and the quota has no effect.
|
|
114
|
+
sessionLimits(options.maxSessions, options.maxSessionsPerOwner);
|
|
115
|
+
}
|
|
116
|
+
// Only HTTP builds the session store, but its options are checked here, so stdio refuses them
|
|
117
|
+
// too. Each of HTTP's own checks below runs where HTTP built what it checks, in the same order.
|
|
118
|
+
if (contextState)
|
|
119
|
+
sessionStoreLimits(sessionStoreOptions(options));
|
|
120
|
+
const registeredCommands = [...validatedCommands];
|
|
54
121
|
if (contextState && contexts) {
|
|
55
122
|
for (const command of createContextCommands(contexts, contextState)) {
|
|
56
123
|
if (registeredCommands.some((existing) => existing.name === command.name)) {
|
|
@@ -64,7 +131,8 @@ function createSharedHttpRuntime(options) {
|
|
|
64
131
|
// afd-help/afd-docs/afd-schema describe what a remote agent can see and call:
|
|
65
132
|
// MCP-exposed commands in the active context (built-ins included).
|
|
66
133
|
// The caller's context state: the HTTP session's, or the single stdio state.
|
|
67
|
-
|
|
134
|
+
// With MCP authorization's filtered listings, only what the caller is shown (M6).
|
|
135
|
+
const describe = (context) => listedCommands(filterByContext(remoteCommands, resolveContextState(context, contextState)?.getActive()), context);
|
|
68
136
|
registeredCommands.push(...getBootstrapCommands(describe, {
|
|
69
137
|
toolSurface: { toolStrategy, firstClassTools, contextFields },
|
|
70
138
|
}));
|
|
@@ -74,30 +142,139 @@ function createSharedHttpRuntime(options) {
|
|
|
74
142
|
for (const cmd of registeredCommands) {
|
|
75
143
|
commandMap.set(cmd.name, cmd);
|
|
76
144
|
}
|
|
145
|
+
const defaultTimeoutMs = resolveDefaultTimeoutMs(options.defaultTimeoutMs);
|
|
146
|
+
const maxValidationIssues = resolveMaxValidationIssues(options.maxValidationIssues);
|
|
147
|
+
if (options.maxBatchSize !== undefined &&
|
|
148
|
+
(!Number.isSafeInteger(options.maxBatchSize) || options.maxBatchSize <= 0)) {
|
|
149
|
+
throw new RangeError('maxBatchSize must be a positive integer');
|
|
150
|
+
}
|
|
151
|
+
if (options.maxBatchParallelism !== undefined &&
|
|
152
|
+
(!Number.isSafeInteger(options.maxBatchParallelism) || options.maxBatchParallelism <= 0)) {
|
|
153
|
+
throw new RangeError('maxBatchParallelism must be a positive integer');
|
|
154
|
+
}
|
|
155
|
+
const { maxBatchSize, maxBatchParallelism } = options;
|
|
77
156
|
const contextDefaults = { clock: options.clock, random: options.random };
|
|
157
|
+
// The idempotency step, one for every engine of this server, so a key reserved on one lane is
|
|
158
|
+
// held on all of them (spec/idempotency.md, S1 to S13). S8 is checked against the default
|
|
159
|
+
// deadline, so a server that would let a key become new while its first call runs is refused.
|
|
160
|
+
const idempotency = options.idempotency === undefined
|
|
161
|
+
? undefined
|
|
162
|
+
: createIdempotencyStep(options.idempotency, {
|
|
163
|
+
clock: options.clock,
|
|
164
|
+
defaultTimeoutMs,
|
|
165
|
+
// Verified credentials under MCP authorization, else `createContext`'s.
|
|
166
|
+
credentialsOf: (context) => scopeViewOf(context)?.credentials ?? context.credentials,
|
|
167
|
+
});
|
|
168
|
+
/**
|
|
169
|
+
* Whether the server tells apart the HTTP caller with `credentials` (S3), as for MCP Tasks:
|
|
170
|
+
* it verifies tokens and the request carries one, or the host declared that `createContext`
|
|
171
|
+
* identifies every caller.
|
|
172
|
+
*/
|
|
173
|
+
const identifiesHttpCaller = (credentials) => (Boolean(options.auth) && credentials !== undefined && credentials !== null) ||
|
|
174
|
+
(Boolean(options.createContext) &&
|
|
175
|
+
(idempotency?.options.callersIdentifiedByHook === true ||
|
|
176
|
+
options.taskOptions?.callersIdentifiedByHook === true));
|
|
177
|
+
const stepDeps = idempotency ? { idempotency, defaultTimeoutMs } : {};
|
|
178
|
+
const commandMiddleware = withCommandDeadlines(middleware, defaultTimeoutMs);
|
|
179
|
+
// Whether the caller is shown a command under MCP authorization (M6): a refusal names it only
|
|
180
|
+
// then. Read here, from the request's scope view, so the engine never loads `scope-view`.
|
|
181
|
+
const isShown = (name, context) => scopeViewOf(context)?.shown(name) ?? true;
|
|
182
|
+
const relayHooks = {
|
|
183
|
+
transfer: transferRelayState,
|
|
184
|
+
takeDispatch(context, commandName, data) {
|
|
185
|
+
if (takeRelayDispatchAdmission(context, commandName, data) !== 'mismatch')
|
|
186
|
+
return undefined;
|
|
187
|
+
return {
|
|
188
|
+
code: 'CONFIRMATION_REQUIRED',
|
|
189
|
+
message: `'${commandName}' needs a person's confirmation through the host`,
|
|
190
|
+
suggestion: 'Do not retry: this call is not the one the person confirmed. Move middleware that retries or changes input before confirmation.',
|
|
191
|
+
retryable: false,
|
|
192
|
+
details: { commands: [commandName], reason: 'admission-mismatch' },
|
|
193
|
+
// The operator fixes the middleware order (`spec/recovery.md`, "Engine errors").
|
|
194
|
+
recovery: { kind: 'external' },
|
|
195
|
+
};
|
|
196
|
+
},
|
|
197
|
+
clear: clearRelayEvidence,
|
|
198
|
+
};
|
|
199
|
+
// The in-process lane (`server.execute`): it reaches commands without the router, so its
|
|
200
|
+
// refusals never name a meta-tool (`spec/recovery.md`, "Engine errors").
|
|
78
201
|
const engine = createExecutionEngine({
|
|
79
202
|
commandMap,
|
|
80
|
-
middleware,
|
|
203
|
+
middleware: commandMiddleware,
|
|
204
|
+
metaTools: false,
|
|
205
|
+
isShown,
|
|
81
206
|
devMode,
|
|
82
207
|
onCommand,
|
|
83
208
|
onError,
|
|
84
209
|
contextDefaults,
|
|
85
210
|
contextFields,
|
|
211
|
+
maxBatchSize,
|
|
212
|
+
maxBatchParallelism,
|
|
213
|
+
maxValidationIssues,
|
|
214
|
+
relay: relayHooks,
|
|
215
|
+
...stepDeps,
|
|
86
216
|
});
|
|
87
217
|
const remoteCommands = registeredCommands.filter((command) => isMcpExposed(command));
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
218
|
+
const remoteCommandMap = new Map(remoteCommands.map((command) => [command.name, command]));
|
|
219
|
+
const remoteDeps = {
|
|
220
|
+
commandMap: remoteCommandMap,
|
|
221
|
+
// MCP authorization (spec/authorization.md, M6): a resolver decides every command again at
|
|
222
|
+
// execution, on its validated input, before any other middleware or the handler; and a
|
|
223
|
+
// command hidden from the caller is looked up as a name that does not exist.
|
|
224
|
+
middleware: options.auth ? [executionScopes, ...commandMiddleware] : commandMiddleware,
|
|
225
|
+
isShown,
|
|
226
|
+
...(options.auth && {
|
|
227
|
+
hidden: (commandName, context) => !(scopeViewOf(context)?.known(commandName) ?? true),
|
|
228
|
+
shown: shownOnly,
|
|
229
|
+
}),
|
|
230
|
+
// A step of an afd-batch or afd-pipe approved up front runs on the input validated then
|
|
231
|
+
// (#1414): what the human was shown, not what a second validation gives (a default that
|
|
232
|
+
// is random or reads the clock differs). The step is still validated when it runs, as sent
|
|
233
|
+
// and on the approved value itself (#1511), since a schema may check state an earlier
|
|
234
|
+
// step changed; only the value is kept.
|
|
235
|
+
validated: async (commandName, input, context) => {
|
|
236
|
+
// A host-verified call already validated its input before calling `HumanRelay`, so
|
|
237
|
+
// it runs on that exact validated object even when a schema default is non-deterministic.
|
|
238
|
+
const prevalidated = takeRelayValidatedInput(context, commandName);
|
|
239
|
+
if (prevalidated)
|
|
240
|
+
return { success: true, data: prevalidated.data, errors: [] };
|
|
241
|
+
const command = remoteCommandMap.get(commandName);
|
|
242
|
+
const approved = command && (await upFrontApproval(context)?.validated(commandName, input));
|
|
243
|
+
if (!approved)
|
|
244
|
+
return undefined;
|
|
245
|
+
const checked = validateInputEnhanced(command.inputSchema, input, maxValidationIssues);
|
|
246
|
+
if (!checked.success)
|
|
247
|
+
return checked;
|
|
248
|
+
const ran = checkApprovedAtRun(command, approved.input, maxValidationIssues);
|
|
249
|
+
return ran.success ? { ...checked, data: approved.input } : ran;
|
|
250
|
+
},
|
|
91
251
|
devMode,
|
|
92
252
|
onCommand,
|
|
93
253
|
onError,
|
|
94
254
|
contextState,
|
|
95
255
|
contextDefaults: { ...contextDefaults, actor: options.actor ?? { kind: 'agent' } },
|
|
96
256
|
contextFields,
|
|
97
|
-
|
|
257
|
+
maxBatchSize,
|
|
258
|
+
maxBatchParallelism,
|
|
259
|
+
maxValidationIssues,
|
|
260
|
+
relay: relayHooks,
|
|
261
|
+
...stepDeps,
|
|
262
|
+
};
|
|
263
|
+
// The MCP lanes: the router serves the meta-tools there, so a refusal may name them.
|
|
264
|
+
const remoteEngine = createExecutionEngine(remoteDeps);
|
|
98
265
|
const exposedCommandNames = new Set(remoteCommands.map((c) => c.name));
|
|
99
266
|
const confirmations = createConfirmationGate(options.confirmations ?? 'off', remoteCommands);
|
|
100
|
-
const
|
|
267
|
+
const defaultActor = options.actor ?? { kind: 'agent' };
|
|
268
|
+
const humanRelay = createHumanRelayGate({
|
|
269
|
+
relay: options.humanRelay,
|
|
270
|
+
commands: remoteCommands,
|
|
271
|
+
actor: defaultActor,
|
|
272
|
+
devMode,
|
|
273
|
+
contextFields,
|
|
274
|
+
clock,
|
|
275
|
+
maxValidationIssues,
|
|
276
|
+
onError,
|
|
277
|
+
});
|
|
101
278
|
const routeToolCall = createToolRouter({
|
|
102
279
|
executeCommand: remoteEngine.executeCommand,
|
|
103
280
|
executeBatch: remoteEngine.executeBatch,
|
|
@@ -110,11 +287,66 @@ function createSharedHttpRuntime(options) {
|
|
|
110
287
|
exposedCommandNames,
|
|
111
288
|
contextState,
|
|
112
289
|
confirmations,
|
|
290
|
+
humanRelay,
|
|
291
|
+
idempotency,
|
|
292
|
+
maxBatchSize,
|
|
293
|
+
maxBatchParallelism,
|
|
113
294
|
});
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
295
|
+
/**
|
|
296
|
+
* What only the HTTP `/rpc`, `/batch` and `/stream` routes use, built when HTTP is served: an
|
|
297
|
+
* engine whose refusals name no meta-tool, since those routes serve none, and only `afd-help`
|
|
298
|
+
* when it is registered and callable (`spec/recovery.md`, "Engine errors"); the same router
|
|
299
|
+
* stages on it; and the mutations GET streaming refuses.
|
|
300
|
+
*/
|
|
301
|
+
const httpRouting = () => {
|
|
302
|
+
const httpEngine = createExecutionEngine({ ...remoteDeps, metaTools: false });
|
|
303
|
+
const restRouter = createToolRouter({
|
|
304
|
+
executeCommand: httpEngine.executeCommand,
|
|
305
|
+
executeBatch: httpEngine.executeBatch,
|
|
306
|
+
executePipeline: httpEngine.executePipeline,
|
|
307
|
+
commands: remoteCommands,
|
|
308
|
+
toolStrategy,
|
|
309
|
+
groupByFn,
|
|
310
|
+
devMode,
|
|
311
|
+
allCommands: remoteCommands,
|
|
312
|
+
exposedCommandNames,
|
|
313
|
+
contextState,
|
|
314
|
+
confirmations,
|
|
315
|
+
humanRelay,
|
|
316
|
+
idempotency,
|
|
317
|
+
maxBatchSize,
|
|
318
|
+
maxBatchParallelism,
|
|
319
|
+
metaTools: false,
|
|
320
|
+
callTracePrefix: 'rpc',
|
|
321
|
+
});
|
|
322
|
+
const mutationNames = new Set(remoteCommands.filter((command) => command.mutation === true).map((command) => command.name));
|
|
323
|
+
return { httpEngine, restRouter, mutationNames };
|
|
324
|
+
};
|
|
325
|
+
// Built once per command and group, and whole for each context when no scope view narrows it.
|
|
326
|
+
const toolsList = createToolsList({
|
|
327
|
+
commands: remoteCommands,
|
|
328
|
+
toolStrategy,
|
|
329
|
+
groupByFn,
|
|
330
|
+
options: {
|
|
331
|
+
outputSchema: options.advertiseOutputSchema,
|
|
332
|
+
confirmations: confirmations.mode,
|
|
333
|
+
firstClassTools,
|
|
334
|
+
},
|
|
335
|
+
contexts: contexts?.map((context) => context.name),
|
|
336
|
+
});
|
|
337
|
+
const boundGetToolsList = (context = {}) => toolsList(listedCommands(remoteCommands, context), resolveContextState(context, contextState)?.getActive());
|
|
338
|
+
// Checked here, so a bad `auth` option fails a stdio server too; the layer is built for HTTP.
|
|
339
|
+
if (options.auth)
|
|
340
|
+
checkAuthOptions(options.auth, remoteCommands);
|
|
341
|
+
const taskRuntime = new TaskRuntime({
|
|
342
|
+
commands: remoteCommands,
|
|
343
|
+
executeCommand: remoteEngine.executeCommand,
|
|
344
|
+
store: options.taskStore,
|
|
345
|
+
options: options.taskOptions,
|
|
346
|
+
clock,
|
|
347
|
+
hasAuth: Boolean(options.auth),
|
|
348
|
+
hasContextHook: Boolean(options.createContext),
|
|
349
|
+
actor: options.actor ?? { kind: 'agent' },
|
|
118
350
|
});
|
|
119
351
|
const modernDeps = {
|
|
120
352
|
name,
|
|
@@ -126,21 +358,82 @@ function createSharedHttpRuntime(options) {
|
|
|
126
358
|
contextFields,
|
|
127
359
|
toolsListTtlMs: options.toolsListTtlMs,
|
|
128
360
|
toolStrategy,
|
|
129
|
-
confirmationKey:
|
|
361
|
+
confirmationKey: instanceState.confirmationKey,
|
|
130
362
|
actor: options.actor ?? { kind: 'agent' },
|
|
363
|
+
approver: options.approver,
|
|
364
|
+
identifiesHttpCaller,
|
|
131
365
|
resources,
|
|
132
366
|
resourcesTtlMs,
|
|
367
|
+
tasks: taskRuntime,
|
|
368
|
+
authConfigured: Boolean(options.auth),
|
|
369
|
+
hasApprovals: middleware.some((mw) => approvalProcessState(mw) !== undefined),
|
|
370
|
+
hasConfirmations: confirmations.mode !== 'off' || Boolean(options.humanRelay),
|
|
371
|
+
hasIdempotency: idempotency !== undefined,
|
|
372
|
+
hasJournal: middleware.some((mw) => isJournalMiddleware(mw)),
|
|
133
373
|
devMode,
|
|
134
374
|
onError,
|
|
135
375
|
};
|
|
376
|
+
// The `/mcp` lane's options, then the handler's (its SSE hub's among them), as HTTP builds
|
|
377
|
+
// them: a stdio server builds neither, yet refuses what an HTTP one would. Stdio reads the TTL.
|
|
378
|
+
resolveToolsListTtlMs(modernDeps);
|
|
379
|
+
checkModernBodyLimit(options.maxBodyBytes ?? 1024 * 1024);
|
|
380
|
+
checkHttpHandlerOptions({
|
|
381
|
+
allowedOrigins: options.allowedOrigins,
|
|
382
|
+
maxBodyBytes: options.maxBodyBytes,
|
|
383
|
+
maxSseConnections: options.maxSseConnections,
|
|
384
|
+
});
|
|
385
|
+
return {
|
|
386
|
+
engine,
|
|
387
|
+
identifiesHttpCaller,
|
|
388
|
+
contextFields,
|
|
389
|
+
routeToolCall,
|
|
390
|
+
httpRouting,
|
|
391
|
+
taskRuntime,
|
|
392
|
+
getToolsList: boundGetToolsList,
|
|
393
|
+
modernDeps,
|
|
394
|
+
contextState,
|
|
395
|
+
remoteCommands,
|
|
396
|
+
httpWarning: instanceState.httpWarning,
|
|
397
|
+
url: `http://${host}:${port}`,
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
/** The HTTP session store's options: sessions exist only with `contexts`. */
|
|
401
|
+
function sessionStoreOptions(options) {
|
|
402
|
+
const { clock } = options;
|
|
403
|
+
return {
|
|
404
|
+
maxSessions: options.maxSessions,
|
|
405
|
+
// Without `auth` every caller is one owner, so its quota is the whole store.
|
|
406
|
+
maxSessionsPerOwner: options.auth
|
|
407
|
+
? options.maxSessionsPerOwner
|
|
408
|
+
: (options.maxSessions ?? DEFAULT_MAX_SESSIONS),
|
|
409
|
+
idleTimeoutMs: options.sessionIdleTimeoutMs,
|
|
410
|
+
...(clock ? { now: () => clock.now() } : {}),
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* What only HTTP serves, on a server's {@link createServerCore}: the session store, the
|
|
415
|
+
* authorization layer, the `/mcp` lane, and the handler with its SSE hub.
|
|
416
|
+
*/
|
|
417
|
+
function createHttpRuntime(core, options) {
|
|
418
|
+
const { name, version, port = 3100, host = 'localhost', devMode = false, cors = false, onError, toolStrategy = 'grouped', groupByFn, maxBatchSize, maxBatchParallelism, clock, } = options;
|
|
419
|
+
const sessions = core.contextState ? createSessionStore(sessionStoreOptions(options)) : undefined;
|
|
136
420
|
const maxBodyBytes = options.maxBodyBytes ?? 1024 * 1024;
|
|
137
|
-
const auth = options.auth
|
|
421
|
+
const auth = options.auth
|
|
422
|
+
? createAuthLayer(options.auth, {
|
|
423
|
+
maxBodyBytes,
|
|
424
|
+
clock,
|
|
425
|
+
commands: core.remoteCommands,
|
|
426
|
+
toolStrategy,
|
|
427
|
+
groupByFn,
|
|
428
|
+
})
|
|
429
|
+
: undefined;
|
|
138
430
|
const modern = createModernHttpLane({
|
|
139
|
-
...modernDeps,
|
|
431
|
+
...core.modernDeps,
|
|
140
432
|
createContext: options.createContext,
|
|
141
433
|
maxBodyBytes,
|
|
142
434
|
auth,
|
|
143
435
|
});
|
|
436
|
+
const { httpEngine, restRouter, mutationNames } = core.httpRouting();
|
|
144
437
|
const { handler, dispose } = createHttpHandler({
|
|
145
438
|
name,
|
|
146
439
|
version,
|
|
@@ -152,30 +445,50 @@ function createSharedHttpRuntime(options) {
|
|
|
152
445
|
allowedOrigins: options.allowedOrigins,
|
|
153
446
|
maxBodyBytes: options.maxBodyBytes,
|
|
154
447
|
maxSseConnections: options.maxSseConnections,
|
|
155
|
-
getToolsList:
|
|
156
|
-
routeToolCall,
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
448
|
+
getToolsList: core.getToolsList,
|
|
449
|
+
routeToolCall: core.routeToolCall,
|
|
450
|
+
approver: options.approver,
|
|
451
|
+
identifiesHttpCaller: core.identifiesHttpCaller,
|
|
452
|
+
actor: options.actor ?? { kind: 'agent' },
|
|
453
|
+
restRouter,
|
|
454
|
+
executeStream: httpEngine.executeStream,
|
|
160
455
|
isMutation: (commandName) => mutationNames.has(commandName),
|
|
161
|
-
confirmations,
|
|
162
456
|
sessions,
|
|
163
457
|
createContext: options.createContext,
|
|
164
|
-
contextFields,
|
|
458
|
+
contextFields: core.contextFields,
|
|
165
459
|
onError,
|
|
166
460
|
modern,
|
|
167
461
|
auth,
|
|
462
|
+
tasks: core.taskRuntime,
|
|
463
|
+
contract: serverContractFor(core.modernDeps, true),
|
|
464
|
+
maxBatchSize,
|
|
465
|
+
maxBatchParallelism,
|
|
168
466
|
});
|
|
467
|
+
return { handler, dispose, auth };
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* The wiring every lane of one server shares, with its HTTP runtime built: engines, routers,
|
|
471
|
+
* admission, the HTTP handler and the stdio lane's dependencies. Exported for tests that drive
|
|
472
|
+
* several lanes of one server; not part of the package's entry points.
|
|
473
|
+
*/
|
|
474
|
+
export function createSharedHttpRuntime(options) {
|
|
475
|
+
const core = createServerCore(options);
|
|
476
|
+
const http = createHttpRuntime(core, options);
|
|
169
477
|
return {
|
|
170
|
-
engine,
|
|
171
|
-
contextFields,
|
|
172
|
-
routeToolCall,
|
|
173
|
-
getToolsList:
|
|
174
|
-
handler,
|
|
175
|
-
dispose
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
478
|
+
engine: core.engine,
|
|
479
|
+
contextFields: core.contextFields,
|
|
480
|
+
routeToolCall: core.routeToolCall,
|
|
481
|
+
getToolsList: core.getToolsList,
|
|
482
|
+
handler: http.handler,
|
|
483
|
+
dispose: () => {
|
|
484
|
+
core.taskRuntime.dispose();
|
|
485
|
+
http.dispose();
|
|
486
|
+
},
|
|
487
|
+
modernDeps: core.modernDeps,
|
|
488
|
+
contextState: core.contextState,
|
|
489
|
+
auth: http.auth,
|
|
490
|
+
httpWarning: core.httpWarning,
|
|
491
|
+
url: core.url,
|
|
179
492
|
};
|
|
180
493
|
}
|
|
181
494
|
/**
|
|
@@ -187,7 +500,22 @@ function createSharedHttpRuntime(options) {
|
|
|
187
500
|
*/
|
|
188
501
|
export function createMcpHandler(options) {
|
|
189
502
|
const runtime = createSharedHttpRuntime(options);
|
|
190
|
-
|
|
503
|
+
const { auth } = runtime;
|
|
504
|
+
warnInstancesOnce(runtime.httpWarning);
|
|
505
|
+
const needsAuth = (what) => new Error(`[AFD] handler.${what} needs the auth option: this handler has no authorization`);
|
|
506
|
+
return Object.assign(runtime.handler, {
|
|
507
|
+
dispose: runtime.dispose,
|
|
508
|
+
admit(req, credentials) {
|
|
509
|
+
if (!auth)
|
|
510
|
+
throw needsAuth('admit');
|
|
511
|
+
auth.hostAdmit(req, credentials);
|
|
512
|
+
},
|
|
513
|
+
preDispatchCheck(checkOptions) {
|
|
514
|
+
if (!auth)
|
|
515
|
+
throw needsAuth('preDispatchCheck');
|
|
516
|
+
return auth.preDispatchCheck(checkOptions);
|
|
517
|
+
},
|
|
518
|
+
});
|
|
191
519
|
}
|
|
192
520
|
/**
|
|
193
521
|
* Create an MCP server from Zod-defined commands.
|
|
@@ -207,7 +535,7 @@ export function createMcpHandler(options) {
|
|
|
207
535
|
* ```
|
|
208
536
|
*/
|
|
209
537
|
export function createMcpServer(options) {
|
|
210
|
-
const { name, version, commands, port = 3100, host = 'localhost', devMode = false, cors =
|
|
538
|
+
const { name, version, commands, port = 3100, host = 'localhost', devMode = false, cors = false, transport = 'auto', middleware = [], onCommand, onError, toolStrategy = 'grouped', groupByFn, contexts, } = options;
|
|
211
539
|
// ── Transport resolution ────────────────────────────────────────────────
|
|
212
540
|
// Removed in 2.0. Fail loudly: silently ignoring it would switch `stdio: false` servers to
|
|
213
541
|
// auto-detection, which picks stdio whenever stdin is piped.
|
|
@@ -225,7 +553,7 @@ export function createMcpServer(options) {
|
|
|
225
553
|
const useHttp = resolvedTransport === 'http';
|
|
226
554
|
const autoDetected = transport === 'auto';
|
|
227
555
|
// ── Shared execution/runtime wiring ─────────────────────────────────────
|
|
228
|
-
const
|
|
556
|
+
const runtimeOptions = {
|
|
229
557
|
...options,
|
|
230
558
|
name,
|
|
231
559
|
version,
|
|
@@ -240,7 +568,14 @@ export function createMcpServer(options) {
|
|
|
240
568
|
toolStrategy,
|
|
241
569
|
groupByFn,
|
|
242
570
|
contexts,
|
|
243
|
-
}
|
|
571
|
+
};
|
|
572
|
+
const core = createServerCore(runtimeOptions);
|
|
573
|
+
// Only HTTP builds the HTTP runtime: stdio serves no session, SSE stream or token (#1746).
|
|
574
|
+
const http = useHttp ? createHttpRuntime(core, runtimeOptions) : undefined;
|
|
575
|
+
const dispose = () => {
|
|
576
|
+
core.taskRuntime.dispose();
|
|
577
|
+
http?.dispose();
|
|
578
|
+
};
|
|
244
579
|
// ── Server state ────────────────────────────────────────────────────────
|
|
245
580
|
let httpServer = null;
|
|
246
581
|
let stdioHandle = null;
|
|
@@ -252,7 +587,7 @@ export function createMcpServer(options) {
|
|
|
252
587
|
return;
|
|
253
588
|
// Stdio via the MCP SDK v2: dual-era (2026-07-28 per-request _meta, or initialize).
|
|
254
589
|
if (useStdio) {
|
|
255
|
-
stdioHandle = serveModernStdio(
|
|
590
|
+
stdioHandle = serveModernStdio(core.modernDeps, core.contextState);
|
|
256
591
|
isRunning = true;
|
|
257
592
|
// stdout carries MCP frames, so the notice goes to stderr.
|
|
258
593
|
console.error(autoDetected
|
|
@@ -262,8 +597,8 @@ export function createMcpServer(options) {
|
|
|
262
597
|
return;
|
|
263
598
|
}
|
|
264
599
|
// HTTP transport
|
|
265
|
-
if (
|
|
266
|
-
httpServer = createServer(
|
|
600
|
+
if (http) {
|
|
601
|
+
httpServer = createServer(http.handler);
|
|
267
602
|
await new Promise((resolve, reject) => {
|
|
268
603
|
httpServer?.on('error', reject);
|
|
269
604
|
httpServer?.listen(port, host, () => {
|
|
@@ -271,18 +606,20 @@ export function createMcpServer(options) {
|
|
|
271
606
|
resolve();
|
|
272
607
|
});
|
|
273
608
|
});
|
|
274
|
-
console.error(`[${name}] MCP transport: http at ${
|
|
609
|
+
console.error(`[${name}] MCP transport: http at ${core.url}${autoDetected ? ' (auto-detected because stdin is a TTY)' : ''}`);
|
|
610
|
+
warnInstancesOnce(core.httpWarning);
|
|
275
611
|
}
|
|
276
612
|
},
|
|
277
613
|
async stop() {
|
|
278
614
|
if (!isRunning)
|
|
279
615
|
return;
|
|
280
616
|
if (stdioHandle) {
|
|
617
|
+
dispose();
|
|
281
618
|
await stdioHandle.close();
|
|
282
619
|
stdioHandle = null;
|
|
283
620
|
}
|
|
284
621
|
if (httpServer) {
|
|
285
|
-
|
|
622
|
+
dispose();
|
|
286
623
|
httpServer.closeAllConnections();
|
|
287
624
|
await new Promise((resolve, reject) => {
|
|
288
625
|
httpServer?.close((err) => {
|
|
@@ -297,17 +634,22 @@ export function createMcpServer(options) {
|
|
|
297
634
|
isRunning = false;
|
|
298
635
|
},
|
|
299
636
|
getUrl() {
|
|
300
|
-
return useHttp ?
|
|
637
|
+
return useHttp ? core.url : 'stdio://';
|
|
301
638
|
},
|
|
302
|
-
contextFields:
|
|
639
|
+
contextFields: core.contextFields,
|
|
303
640
|
getCommands() {
|
|
304
641
|
return commands;
|
|
305
642
|
},
|
|
306
643
|
getTransport() {
|
|
307
644
|
return resolvedTransport;
|
|
308
645
|
},
|
|
309
|
-
|
|
310
|
-
|
|
646
|
+
// An app's call needs a grant check this server does not have (`createAppCallRegistry`).
|
|
647
|
+
execute: (name, input, context) => context?.interface === 'apps'
|
|
648
|
+
? Promise.resolve(failure(callNotGranted()))
|
|
649
|
+
: core.engine.executeCommand(name, input, context),
|
|
650
|
+
executePipeline: (request, context) => context?.interface === 'apps'
|
|
651
|
+
? executeCorePipeline(request, async () => failure(callNotGranted()))
|
|
652
|
+
: core.engine.executePipeline(request, context),
|
|
311
653
|
};
|
|
312
654
|
}
|
|
313
655
|
//# sourceMappingURL=server.js.map
|