@lushly-dev/afd-server 2.5.0 → 3.1.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 +100 -21
- 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 +2 -2
- package/dist/app-call-registry.d.ts.map +1 -1
- package/dist/app-call-registry.js +12 -4
- package/dist/app-call-registry.js.map +1 -1
- package/dist/approval-admission.d.ts +47 -1
- package/dist/approval-admission.d.ts.map +1 -1
- package/dist/approval-admission.js +87 -0
- package/dist/approval-admission.js.map +1 -1
- package/dist/approval-crypto.d.ts +16 -2
- package/dist/approval-crypto.d.ts.map +1 -1
- package/dist/approval-crypto.js +45 -11
- package/dist/approval-crypto.js.map +1 -1
- package/dist/approval-process-state.d.ts +3 -0
- package/dist/approval-process-state.d.ts.map +1 -1
- package/dist/approval-process-state.js +2 -0
- package/dist/approval-process-state.js.map +1 -1
- package/dist/approval-relay.d.ts +41 -5
- package/dist/approval-relay.d.ts.map +1 -1
- package/dist/approval-relay.js +8 -11
- package/dist/approval-relay.js.map +1 -1
- package/dist/approval-up-front.d.ts.map +1 -1
- package/dist/approval-up-front.js +62 -13
- package/dist/approval-up-front.js.map +1 -1
- package/dist/approval.d.ts +55 -3
- package/dist/approval.d.ts.map +1 -1
- package/dist/approval.js +208 -54
- package/dist/approval.js.map +1 -1
- package/dist/approver.d.ts +2 -2
- package/dist/approver.d.ts.map +1 -1
- package/dist/approver.js +6 -6
- package/dist/approver.js.map +1 -1
- 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-help.d.ts +3 -1
- package/dist/bootstrap/afd-help.d.ts.map +1 -1
- package/dist/bootstrap/afd-help.js +2 -0
- package/dist/bootstrap/afd-help.js.map +1 -1
- package/dist/bootstrap/described-command.d.ts +2 -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.map +1 -1
- package/dist/command-timeout.js +8 -1
- package/dist/command-timeout.js.map +1 -1
- package/dist/confirmation-gate.d.ts +7 -7
- package/dist/confirmation-gate.d.ts.map +1 -1
- package/dist/confirmation-gate.js +44 -26
- package/dist/confirmation-gate.js.map +1 -1
- package/dist/confirmations.d.ts +46 -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 +21 -4
- 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 +39 -3
- package/dist/define-core.d.ts.map +1 -1
- package/dist/define-core.js +20 -4
- package/dist/define-core.js.map +1 -1
- package/dist/define.d.ts +2 -2
- package/dist/define.d.ts.map +1 -1
- package/dist/define.js.map +1 -1
- package/dist/direct-registry.d.ts +30 -8
- package/dist/direct-registry.d.ts.map +1 -1
- package/dist/direct-registry.js +26 -5
- package/dist/direct-registry.js.map +1 -1
- package/dist/direct-tool-surface.d.ts +3 -1
- package/dist/direct-tool-surface.d.ts.map +1 -1
- package/dist/direct-tool-surface.js +22 -5
- package/dist/direct-tool-surface.js.map +1 -1
- package/dist/direct.d.ts +4 -2
- package/dist/direct.d.ts.map +1 -1
- package/dist/direct.js +2 -0
- package/dist/direct.js.map +1 -1
- package/dist/execution.d.ts +35 -6
- package/dist/execution.d.ts.map +1 -1
- package/dist/execution.js +108 -15
- package/dist/execution.js.map +1 -1
- package/dist/frontmatter.js +16 -13
- package/dist/frontmatter.js.map +1 -1
- package/dist/host-state.d.ts +96 -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 +31 -12
- package/dist/http-handler.d.ts.map +1 -1
- package/dist/http-handler.js +138 -102
- package/dist/http-handler.js.map +1 -1
- package/dist/http-security.js +2 -2
- package/dist/http-security.js.map +1 -1
- package/dist/http-sessions.d.ts +10 -0
- package/dist/http-sessions.d.ts.map +1 -1
- package/dist/http-sessions.js +12 -2
- 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 +697 -0
- package/dist/human-relay.js.map +1 -0
- package/dist/idempotency-step.d.ts +243 -0
- package/dist/idempotency-step.d.ts.map +1 -0
- package/dist/idempotency-step.js +419 -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 +10 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -5
- package/dist/index.js.map +1 -1
- package/dist/instance-state.d.ts +7 -0
- package/dist/instance-state.d.ts.map +1 -1
- package/dist/instance-state.js +5 -2
- package/dist/instance-state.js.map +1 -1
- 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 +7 -0
- 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 +24 -5
- package/dist/journal.d.ts.map +1 -1
- package/dist/journal.js +91 -40
- package/dist/journal.js.map +1 -1
- package/dist/lazy-tools.d.ts +13 -3
- package/dist/lazy-tools.d.ts.map +1 -1
- package/dist/lazy-tools.js +9 -2
- package/dist/lazy-tools.js.map +1 -1
- package/dist/mcp-auth-dispatch.d.ts +1 -1
- package/dist/mcp-auth-dispatch.d.ts.map +1 -1
- package/dist/mcp-auth-dispatch.js +2 -1
- package/dist/mcp-auth-dispatch.js.map +1 -1
- package/dist/mcp-auth-jwt.d.ts +11 -4
- package/dist/mcp-auth-jwt.d.ts.map +1 -1
- package/dist/mcp-auth-jwt.js +11 -9
- package/dist/mcp-auth-jwt.js.map +1 -1
- package/dist/mcp-auth-resource.d.ts +27 -3
- package/dist/mcp-auth-resource.d.ts.map +1 -1
- package/dist/mcp-auth-resource.js +37 -10
- package/dist/mcp-auth-resource.js.map +1 -1
- package/dist/mcp-auth-rules.d.ts +97 -18
- package/dist/mcp-auth-rules.d.ts.map +1 -1
- package/dist/mcp-auth-rules.js +194 -37
- package/dist/mcp-auth-rules.js.map +1 -1
- package/dist/mcp-auth-view.d.ts +3 -3
- package/dist/mcp-auth-view.d.ts.map +1 -1
- package/dist/mcp-auth-view.js +62 -25
- package/dist/mcp-auth-view.js.map +1 -1
- package/dist/mcp-auth.d.ts +27 -4
- package/dist/mcp-auth.d.ts.map +1 -1
- package/dist/mcp-auth.js +55 -14
- package/dist/mcp-auth.js.map +1 -1
- package/dist/mcp-modern.d.ts +70 -7
- package/dist/mcp-modern.d.ts.map +1 -1
- package/dist/mcp-modern.js +225 -111
- package/dist/mcp-modern.js.map +1 -1
- package/dist/meta-argument.d.ts +2 -1
- package/dist/meta-argument.d.ts.map +1 -1
- package/dist/meta-argument.js +13 -2
- 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 +18 -3
- package/dist/middleware.d.ts.map +1 -1
- package/dist/middleware.js +53 -7
- package/dist/middleware.js.map +1 -1
- package/dist/output-budget.d.ts.map +1 -1
- package/dist/output-budget.js +2 -0
- package/dist/output-budget.js.map +1 -1
- 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/request-context.d.ts +4 -0
- package/dist/request-context.d.ts.map +1 -1
- package/dist/request-context.js +12 -2
- package/dist/request-context.js.map +1 -1
- package/dist/resources.js +1 -1
- 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 +18 -5
- package/dist/scope-view.d.ts.map +1 -1
- package/dist/scope-view.js +9 -8
- package/dist/scope-view.js.map +1 -1
- package/dist/server-types.d.ts +44 -16
- 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 +359 -89
- package/dist/server.js.map +1 -1
- package/dist/task-dispatch.d.ts +12 -6
- package/dist/task-dispatch.d.ts.map +1 -1
- package/dist/task-dispatch.js +11 -12
- package/dist/task-dispatch.js.map +1 -1
- package/dist/task-input.d.ts +29 -0
- package/dist/task-input.d.ts.map +1 -0
- package/dist/task-input.js +75 -0
- package/dist/task-input.js.map +1 -0
- package/dist/tasks-input-gate.d.ts +61 -0
- package/dist/tasks-input-gate.d.ts.map +1 -0
- package/dist/tasks-input-gate.js +325 -0
- package/dist/tasks-input-gate.js.map +1 -0
- package/dist/tasks-input-runner.d.ts +125 -0
- package/dist/tasks-input-runner.d.ts.map +1 -0
- package/dist/tasks-input-runner.js +1017 -0
- package/dist/tasks-input-runner.js.map +1 -0
- package/dist/tasks-runtime.d.ts +34 -7
- package/dist/tasks-runtime.d.ts.map +1 -1
- package/dist/tasks-runtime.js +545 -94
- package/dist/tasks-runtime.js.map +1 -1
- package/dist/tasks.d.ts +196 -25
- package/dist/tasks.d.ts.map +1 -1
- package/dist/tasks.js +188 -26
- package/dist/tasks.js.map +1 -1
- package/dist/tool-router.d.ts +62 -3
- package/dist/tool-router.d.ts.map +1 -1
- package/dist/tool-router.js +251 -101
- 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 +154 -40
- package/dist/tools.js.map +1 -1
- package/dist/up-front-steps.d.ts +8 -0
- package/dist/up-front-steps.d.ts.map +1 -1
- package/dist/up-front-steps.js +50 -1
- package/dist/up-front-steps.js.map +1 -1
- package/dist/validation.d.ts +14 -0
- package/dist/validation.d.ts.map +1 -1
- package/dist/validation.js +62 -17
- package/dist/validation.js.map +1 -1
- package/package.json +15 -11
package/dist/server.js
CHANGED
|
@@ -8,7 +8,11 @@ import { createServer } from 'node:http';
|
|
|
8
8
|
import { executePipeline as executeCorePipeline, failure, isMcpExposed, } from '@lushly-dev/afd-core';
|
|
9
9
|
import { validateOutputBudget } from '@lushly-dev/afd-core/commands';
|
|
10
10
|
import { callNotGranted } from '@lushly-dev/afd-core/internal/app-calls';
|
|
11
|
+
import { needsAdmission } from './approval-admission.js';
|
|
12
|
+
import { canonicalOrUndefined } from './approval-crypto.js';
|
|
13
|
+
import { approvalProcessState } from './approval-process-state.js';
|
|
11
14
|
import { upFrontApproval } from './approval-relay.js';
|
|
15
|
+
import { assertAuthMiddlewareRecognisesTokens } from './auth-middleware-check.js';
|
|
12
16
|
import { createContextCommands, getBootstrapCommands } from './bootstrap/registry.js';
|
|
13
17
|
import { assertValidCommandNames, assertValidFirstClassTools } from './command-names.js';
|
|
14
18
|
import { filterByContext } from './command-routing.js';
|
|
@@ -17,19 +21,27 @@ import { createConfirmationGate } from './confirmation-gate.js';
|
|
|
17
21
|
import { checkContextFields } from './context-fields.js';
|
|
18
22
|
import { createContextState, resolveContextState } from './context-scope.js';
|
|
19
23
|
import { createExecutionEngine } from './execution.js';
|
|
20
|
-
import {
|
|
21
|
-
import {
|
|
24
|
+
import { hostState, patchHostStateInPlace } from './host-state.js';
|
|
25
|
+
import { checkHttpHandlerOptions, createHttpHandler } from './http-handler.js';
|
|
26
|
+
import { createSessionStore, DEFAULT_MAX_SESSIONS, sessionLimits, sessionStoreLimits, } from './http-sessions.js';
|
|
27
|
+
import { createHumanRelayGate } from './human-relay.js';
|
|
28
|
+
import { createIdempotencyStep } from './idempotency-step.js';
|
|
29
|
+
import { InMemoryIdempotencyStore } from './idempotency-store.js';
|
|
22
30
|
import { checkInstanceState, warnInstancesOnce } from './instance-state.js';
|
|
23
|
-
import {
|
|
24
|
-
import {
|
|
31
|
+
import { isJournalMiddleware } from './journal.js';
|
|
32
|
+
import { checkAuthOptions, createAuthLayer, } from './mcp-auth.js';
|
|
33
|
+
import { checkModernBodyLimit, createModernHttpLane, DEFAULT_TOOLS_LIST_TTL_MS, defaultInstructions, resolveToolsListTtlMs, serveModernStdio, serverContractFor, } from './mcp-modern.js';
|
|
25
34
|
import { assertToolMeta } from './meta-keys.js';
|
|
35
|
+
import { clearRelayEvidence, takeRelayDispatchAdmission, takeRelayValidatedInput, transferRelayState, } from './relay-evidence.js';
|
|
26
36
|
import { createResourceService } from './resources.js';
|
|
27
37
|
import { listedCommands, scopeViewOf, shownOnly } from './scope-view.js';
|
|
28
38
|
import { isStdinPiped } from './server-types.js';
|
|
29
|
-
import {
|
|
39
|
+
import { checkTaskInput } from './task-input.js';
|
|
40
|
+
import { assertValidTaskCommands, taskInputAllowed } from './tasks.js';
|
|
30
41
|
import { TaskRuntime } from './tasks-runtime.js';
|
|
31
42
|
import { createToolRouter } from './tool-router.js';
|
|
32
|
-
import {
|
|
43
|
+
import { createToolsList } from './tools.js';
|
|
44
|
+
import { checkApprovedAtRun } from './up-front-steps.js';
|
|
33
45
|
import { resolveMaxValidationIssues, validateInputEnhanced } from './validation.js';
|
|
34
46
|
// Re-export public types so existing imports from './server.js' still work
|
|
35
47
|
export { isStdinPiped, MAX_FIRST_CLASS_TOOLS } from './server-types.js';
|
|
@@ -48,16 +60,30 @@ const executionScopes = async (commandName, input, context, next) => {
|
|
|
48
60
|
? failure(view.insufficientScope(decision.forbidden))
|
|
49
61
|
: next();
|
|
50
62
|
};
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
63
|
+
/**
|
|
64
|
+
* The wiring every lane of one server shares, stdio's included: engines, routers, the tools list
|
|
65
|
+
* and the MCP lanes' dependencies. It builds nothing HTTP-only (#1746): that is
|
|
66
|
+
* {@link createHttpRuntime}'s, which also calls `httpRouting` for the HTTP routes' engine and
|
|
67
|
+
* router. Every option either one reads is checked here, so a server that would fail over HTTP
|
|
68
|
+
* fails to build over stdio too.
|
|
69
|
+
*/
|
|
70
|
+
function createServerCore(options) {
|
|
71
|
+
const { name, version, commands, port = 3100, host = 'localhost', devMode = false, middleware = [], onCommand, onError, toolStrategy = 'individual', groupByFn, contexts, bootstrap = false, } = options;
|
|
72
|
+
// Checked first: a server that would serve every token's call as the local session's user does
|
|
73
|
+
// not start (afd-auth's middleware without `isVerifiedCredentials` beside `auth`), nor one that
|
|
74
|
+
// would depend on one process when told it has several (`instances`).
|
|
75
|
+
assertAuthMiddlewareRecognisesTokens(options.auth !== undefined, options.middleware);
|
|
55
76
|
const instanceState = checkInstanceState({
|
|
56
77
|
name,
|
|
57
78
|
instances: options.instances,
|
|
58
79
|
confirmationKey: options.confirmationKey,
|
|
59
80
|
confirmations: options.confirmations,
|
|
81
|
+
hasHumanRelay: Boolean(options.humanRelay),
|
|
60
82
|
middleware,
|
|
83
|
+
// The default store, or one given explicitly, keeps keys in this process only (S13).
|
|
84
|
+
perProcessIdempotency: options.idempotency !== undefined &&
|
|
85
|
+
(options.idempotency.store === undefined ||
|
|
86
|
+
options.idempotency.store instanceof InMemoryIdempotencyStore),
|
|
61
87
|
});
|
|
62
88
|
// A copy: the set must not change while the server runs.
|
|
63
89
|
const firstClassTools = options.firstClassTools ? [...options.firstClassTools] : undefined;
|
|
@@ -66,11 +92,19 @@ function createSharedHttpRuntime(options) {
|
|
|
66
92
|
// and snapshots the validated budget so later mutation of the input object cannot alter it.
|
|
67
93
|
const validatedCommands = commands.map((command) => {
|
|
68
94
|
assertToolMeta(command.name, command.toolMeta);
|
|
95
|
+
if (command.idempotency !== undefined &&
|
|
96
|
+
command.idempotency !== 'natural' &&
|
|
97
|
+
command.idempotency !== 'keyed') {
|
|
98
|
+
throw new Error(`command "${command.name}": idempotency must be 'natural' or 'keyed' (spec/idempotency.md, D1)`);
|
|
99
|
+
}
|
|
69
100
|
return command.outputBudget !== undefined
|
|
70
101
|
? { ...command, outputBudget: validateOutputBudget(command.outputBudget, command.name) }
|
|
71
102
|
: command;
|
|
72
103
|
});
|
|
73
|
-
|
|
104
|
+
// Read once: registration and the task runtime must agree on whether this server asks a person
|
|
105
|
+
// in a task, even when a host's store answers `keepsInput` through a getter.
|
|
106
|
+
const allowTaskInput = taskInputAllowed(options.taskOptions, options.taskStore);
|
|
107
|
+
assertValidTaskCommands(validatedCommands, { allowInput: allowTaskInput });
|
|
74
108
|
// Resource entries and the skill catalog are checked now, so a bad one stops the server from
|
|
75
109
|
// being built. Their results do not vary by context, so contexts leave the TTL alone.
|
|
76
110
|
const resourcesTtlMs = options.toolsListTtlMs ?? DEFAULT_TOOLS_LIST_TTL_MS;
|
|
@@ -86,17 +120,10 @@ function createSharedHttpRuntime(options) {
|
|
|
86
120
|
// Checked even without `contexts`, where no session is issued and the quota has no effect.
|
|
87
121
|
sessionLimits(options.maxSessions, options.maxSessionsPerOwner);
|
|
88
122
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
maxSessionsPerOwner: options.auth
|
|
94
|
-
? options.maxSessionsPerOwner
|
|
95
|
-
: (options.maxSessions ?? DEFAULT_MAX_SESSIONS),
|
|
96
|
-
idleTimeoutMs: options.sessionIdleTimeoutMs,
|
|
97
|
-
...(clock ? { now: () => clock.now() } : {}),
|
|
98
|
-
})
|
|
99
|
-
: undefined;
|
|
123
|
+
// Only HTTP builds the session store, but its options are checked here, so stdio refuses them
|
|
124
|
+
// too. Each of HTTP's own checks below runs where HTTP built what it checks, in the same order.
|
|
125
|
+
if (contextState)
|
|
126
|
+
sessionStoreLimits(sessionStoreOptions(options));
|
|
100
127
|
const registeredCommands = [...validatedCommands];
|
|
101
128
|
if (contextState && contexts) {
|
|
102
129
|
for (const command of createContextCommands(contexts, contextState)) {
|
|
@@ -134,10 +161,98 @@ function createSharedHttpRuntime(options) {
|
|
|
134
161
|
}
|
|
135
162
|
const { maxBatchSize, maxBatchParallelism } = options;
|
|
136
163
|
const contextDefaults = { clock: options.clock, random: options.random };
|
|
164
|
+
// The idempotency step, one for every engine of this server, so a key reserved on one lane is
|
|
165
|
+
// held on all of them (spec/idempotency.md, S1 to S13). S8 is checked against the default
|
|
166
|
+
// deadline, so a server that would let a key become new while its first call runs is refused.
|
|
167
|
+
const idempotency = options.idempotency === undefined
|
|
168
|
+
? undefined
|
|
169
|
+
: createIdempotencyStep(options.idempotency, {
|
|
170
|
+
clock: options.clock,
|
|
171
|
+
defaultTimeoutMs,
|
|
172
|
+
// Verified credentials under MCP authorization, else `createContext`'s.
|
|
173
|
+
credentialsOf: (context) => scopeViewOf(context)?.credentials ?? context.credentials,
|
|
174
|
+
});
|
|
175
|
+
/**
|
|
176
|
+
* Whether the server tells apart the HTTP caller with `credentials` (S3), as for MCP Tasks:
|
|
177
|
+
* it verifies tokens and the request carries one, or the host declared that `createContext`
|
|
178
|
+
* identifies every caller.
|
|
179
|
+
*/
|
|
180
|
+
const identifiesHttpCaller = (credentials) => (Boolean(options.auth) && credentials !== undefined && credentials !== null) ||
|
|
181
|
+
(Boolean(options.createContext) &&
|
|
182
|
+
(idempotency?.options.callersIdentifiedByHook === true ||
|
|
183
|
+
options.taskOptions?.callersIdentifiedByHook === true));
|
|
184
|
+
const stepDeps = idempotency ? { idempotency, defaultTimeoutMs } : {};
|
|
137
185
|
const commandMiddleware = withCommandDeadlines(middleware, defaultTimeoutMs);
|
|
186
|
+
// Whether the caller is shown a command under MCP authorization (M6): a refusal names it only
|
|
187
|
+
// then. Read here, from the request's scope view, so the engine never loads `scope-view`.
|
|
188
|
+
const isShown = (name, context) => scopeViewOf(context)?.shown(name) ?? true;
|
|
189
|
+
const relayHooks = {
|
|
190
|
+
transfer: transferRelayState,
|
|
191
|
+
async takeDispatch(context, commandName, data) {
|
|
192
|
+
const relayAdmission = takeRelayDispatchAdmission(context, commandName, data);
|
|
193
|
+
const relayMismatchError = () => ({
|
|
194
|
+
code: 'CONFIRMATION_REQUIRED',
|
|
195
|
+
message: `'${commandName}' needs a person's confirmation through the host`,
|
|
196
|
+
suggestion: 'Do not retry: this call is not the one the person confirmed. Move middleware that retries or changes input before confirmation.',
|
|
197
|
+
retryable: false,
|
|
198
|
+
details: { commands: [commandName], reason: 'admission-mismatch' },
|
|
199
|
+
// The operator fixes the middleware order (`spec/recovery.md`, "Engine errors").
|
|
200
|
+
recovery: { kind: 'external' },
|
|
201
|
+
});
|
|
202
|
+
if (relayAdmission === 'mismatch') {
|
|
203
|
+
return relayMismatchError();
|
|
204
|
+
}
|
|
205
|
+
// Atomic `handler-started` barrier for MCP tasks (`spec/tasks.md`): cleared in place so
|
|
206
|
+
// nested in-process calls never trigger it again.
|
|
207
|
+
const hs = hostState(context);
|
|
208
|
+
const beforeTaskHandler = hs.beforeTaskHandler;
|
|
209
|
+
if (beforeTaskHandler) {
|
|
210
|
+
const hadApprovalAdmission = needsAdmission(context);
|
|
211
|
+
const snap = hs.taskInputSnapshot;
|
|
212
|
+
const beforeCanonical = relayAdmission === 'admitted' || hadApprovalAdmission || snap !== undefined
|
|
213
|
+
? canonicalOrUndefined(data)
|
|
214
|
+
: undefined;
|
|
215
|
+
patchHostStateInPlace(context, {
|
|
216
|
+
beforeTaskHandler: undefined,
|
|
217
|
+
taskInputSnapshot: undefined,
|
|
218
|
+
});
|
|
219
|
+
if (!(await beforeTaskHandler())) {
|
|
220
|
+
return {
|
|
221
|
+
code: 'COMMAND_CANCELLED',
|
|
222
|
+
message: `${commandName} was cancelled before it started`,
|
|
223
|
+
suggestion: 'Start the task again if the work is still needed.',
|
|
224
|
+
retryable: false,
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
// Final synchronous verification after the store await so input mutated while
|
|
228
|
+
// `handler-started` was in flight can never reach the handler.
|
|
229
|
+
if (beforeCanonical !== undefined && canonicalOrUndefined(data) !== beforeCanonical) {
|
|
230
|
+
if (relayAdmission === 'admitted')
|
|
231
|
+
return relayMismatchError();
|
|
232
|
+
if (hadApprovalAdmission) {
|
|
233
|
+
return {
|
|
234
|
+
code: 'APPROVAL_UNAVAILABLE',
|
|
235
|
+
message: `${commandName} needs a person's approval, and this server cannot ask for it`,
|
|
236
|
+
suggestion: 'Do not retry: this call is not the one approved. The operator moves middleware that retries or changes input before the approval middleware.',
|
|
237
|
+
retryable: false,
|
|
238
|
+
recovery: { kind: 'external' },
|
|
239
|
+
details: { reason: 'admission-mismatch' },
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
return relayMismatchError();
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return undefined;
|
|
246
|
+
},
|
|
247
|
+
clear: clearRelayEvidence,
|
|
248
|
+
};
|
|
249
|
+
// The in-process lane (`server.execute`): it reaches commands without the router, so its
|
|
250
|
+
// refusals never name a meta-tool (`spec/recovery.md`, "Engine errors").
|
|
138
251
|
const engine = createExecutionEngine({
|
|
139
252
|
commandMap,
|
|
140
253
|
middleware: commandMiddleware,
|
|
254
|
+
metaTools: false,
|
|
255
|
+
isShown,
|
|
141
256
|
devMode,
|
|
142
257
|
onCommand,
|
|
143
258
|
onError,
|
|
@@ -146,31 +261,44 @@ function createSharedHttpRuntime(options) {
|
|
|
146
261
|
maxBatchSize,
|
|
147
262
|
maxBatchParallelism,
|
|
148
263
|
maxValidationIssues,
|
|
264
|
+
relay: relayHooks,
|
|
265
|
+
...stepDeps,
|
|
149
266
|
});
|
|
150
267
|
const remoteCommands = registeredCommands.filter((command) => isMcpExposed(command));
|
|
151
268
|
const remoteCommandMap = new Map(remoteCommands.map((command) => [command.name, command]));
|
|
152
|
-
const
|
|
269
|
+
const serverValidated = async (commandName, input, context) => {
|
|
270
|
+
// A host-verified call already validated its input before calling `HumanRelay`, so
|
|
271
|
+
// it runs on that exact validated object even when a schema default is non-deterministic.
|
|
272
|
+
const prevalidated = takeRelayValidatedInput(context, commandName);
|
|
273
|
+
if (prevalidated)
|
|
274
|
+
return { success: true, data: prevalidated.data, errors: [] };
|
|
275
|
+
const command = remoteCommandMap.get(commandName);
|
|
276
|
+
const approved = command && (await upFrontApproval(context)?.validated(commandName, input));
|
|
277
|
+
if (!approved)
|
|
278
|
+
return undefined;
|
|
279
|
+
const checked = validateInputEnhanced(command.inputSchema, input, maxValidationIssues);
|
|
280
|
+
if (!checked.success)
|
|
281
|
+
return checked;
|
|
282
|
+
const ran = checkApprovedAtRun(command, approved.input, maxValidationIssues);
|
|
283
|
+
return ran.success ? { ...checked, data: approved.input } : ran;
|
|
284
|
+
};
|
|
285
|
+
const remoteDeps = {
|
|
153
286
|
commandMap: remoteCommandMap,
|
|
154
287
|
// MCP authorization (spec/authorization.md, M6): a resolver decides every command again at
|
|
155
288
|
// execution, on its validated input, before any other middleware or the handler; and a
|
|
156
289
|
// command hidden from the caller is looked up as a name that does not exist.
|
|
157
290
|
middleware: options.auth ? [executionScopes, ...commandMiddleware] : commandMiddleware,
|
|
291
|
+
isShown,
|
|
158
292
|
...(options.auth && {
|
|
159
293
|
hidden: (commandName, context) => !(scopeViewOf(context)?.known(commandName) ?? true),
|
|
160
294
|
shown: shownOnly,
|
|
161
295
|
}),
|
|
162
296
|
// A step of an afd-batch or afd-pipe approved up front runs on the input validated then
|
|
163
297
|
// (#1414): what the human was shown, not what a second validation gives (a default that
|
|
164
|
-
// is random or reads the clock differs). The step is still validated as sent
|
|
165
|
-
//
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
const approved = command && (await upFrontApproval(context)?.validated(commandName, input));
|
|
169
|
-
if (!approved)
|
|
170
|
-
return undefined;
|
|
171
|
-
const checked = validateInputEnhanced(command.inputSchema, input, maxValidationIssues);
|
|
172
|
-
return checked.success ? { ...checked, data: approved.input } : checked;
|
|
173
|
-
},
|
|
298
|
+
// is random or reads the clock differs). The step is still validated when it runs, as sent
|
|
299
|
+
// and on the approved value itself (#1511), since a schema may check state an earlier
|
|
300
|
+
// step changed; only the value is kept.
|
|
301
|
+
validated: serverValidated,
|
|
174
302
|
devMode,
|
|
175
303
|
onCommand,
|
|
176
304
|
onError,
|
|
@@ -180,10 +308,24 @@ function createSharedHttpRuntime(options) {
|
|
|
180
308
|
maxBatchSize,
|
|
181
309
|
maxBatchParallelism,
|
|
182
310
|
maxValidationIssues,
|
|
183
|
-
|
|
311
|
+
relay: relayHooks,
|
|
312
|
+
...stepDeps,
|
|
313
|
+
};
|
|
314
|
+
// The MCP lanes: the router serves the meta-tools there, so a refusal may name them.
|
|
315
|
+
const remoteEngine = createExecutionEngine(remoteDeps);
|
|
184
316
|
const exposedCommandNames = new Set(remoteCommands.map((c) => c.name));
|
|
185
317
|
const confirmations = createConfirmationGate(options.confirmations ?? 'off', remoteCommands);
|
|
186
|
-
const
|
|
318
|
+
const defaultActor = options.actor ?? { kind: 'agent' };
|
|
319
|
+
const humanRelay = createHumanRelayGate({
|
|
320
|
+
relay: options.humanRelay,
|
|
321
|
+
commands: remoteCommands,
|
|
322
|
+
actor: defaultActor,
|
|
323
|
+
devMode,
|
|
324
|
+
contextFields,
|
|
325
|
+
clock,
|
|
326
|
+
maxValidationIssues,
|
|
327
|
+
onError,
|
|
328
|
+
});
|
|
187
329
|
const routeToolCall = createToolRouter({
|
|
188
330
|
executeCommand: remoteEngine.executeCommand,
|
|
189
331
|
executeBatch: remoteEngine.executeBatch,
|
|
@@ -196,33 +338,85 @@ function createSharedHttpRuntime(options) {
|
|
|
196
338
|
exposedCommandNames,
|
|
197
339
|
contextState,
|
|
198
340
|
confirmations,
|
|
341
|
+
humanRelay,
|
|
342
|
+
idempotency,
|
|
199
343
|
maxBatchSize,
|
|
200
344
|
maxBatchParallelism,
|
|
201
345
|
});
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
const
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
346
|
+
/**
|
|
347
|
+
* What only the HTTP `/rpc`, `/batch` and `/stream` routes use, built when HTTP is served: an
|
|
348
|
+
* engine whose refusals name no meta-tool, since those routes serve none, and only `afd-help`
|
|
349
|
+
* when it is registered and callable (`spec/recovery.md`, "Engine errors"); the same router
|
|
350
|
+
* stages on it; and the mutations GET streaming refuses.
|
|
351
|
+
*/
|
|
352
|
+
const httpRouting = () => {
|
|
353
|
+
const httpEngine = createExecutionEngine({ ...remoteDeps, metaTools: false });
|
|
354
|
+
const restRouter = createToolRouter({
|
|
355
|
+
executeCommand: httpEngine.executeCommand,
|
|
356
|
+
executeBatch: httpEngine.executeBatch,
|
|
357
|
+
executePipeline: httpEngine.executePipeline,
|
|
212
358
|
commands: remoteCommands,
|
|
213
359
|
toolStrategy,
|
|
214
360
|
groupByFn,
|
|
215
|
-
|
|
216
|
-
|
|
361
|
+
devMode,
|
|
362
|
+
allCommands: remoteCommands,
|
|
363
|
+
exposedCommandNames,
|
|
364
|
+
contextState,
|
|
365
|
+
confirmations,
|
|
366
|
+
humanRelay,
|
|
367
|
+
idempotency,
|
|
368
|
+
maxBatchSize,
|
|
369
|
+
maxBatchParallelism,
|
|
370
|
+
metaTools: false,
|
|
371
|
+
});
|
|
372
|
+
const mutationNames = new Set(remoteCommands.filter((command) => command.mutation === true).map((command) => command.name));
|
|
373
|
+
return { httpEngine, restRouter, mutationNames };
|
|
374
|
+
};
|
|
375
|
+
// Built once per command and group, and whole for each context when no scope view narrows it.
|
|
376
|
+
const toolsList = createToolsList({
|
|
377
|
+
commands: remoteCommands,
|
|
378
|
+
toolStrategy,
|
|
379
|
+
groupByFn,
|
|
380
|
+
options: {
|
|
381
|
+
outputSchema: options.advertiseOutputSchema,
|
|
382
|
+
confirmations: confirmations.mode,
|
|
383
|
+
firstClassTools,
|
|
384
|
+
},
|
|
385
|
+
contexts: contexts?.map((context) => context.name),
|
|
386
|
+
});
|
|
387
|
+
const boundGetToolsList = (context = {}) => toolsList(listedCommands(remoteCommands, context), resolveContextState(context, contextState)?.getActive());
|
|
388
|
+
// Checked here, so a bad `auth` option fails a stdio server too; the layer is built for HTTP.
|
|
389
|
+
if (options.auth)
|
|
390
|
+
checkAuthOptions(options.auth, remoteCommands);
|
|
391
|
+
const listedApprovalCommands = new Set();
|
|
392
|
+
for (const mw of middleware) {
|
|
393
|
+
for (const cmdName of approvalProcessState(mw)?.listedGatedCommands ?? []) {
|
|
394
|
+
listedApprovalCommands.add(cmdName);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
assertValidTaskCommands(validatedCommands, {
|
|
398
|
+
allowInput: allowTaskInput,
|
|
399
|
+
listedApprovalCommands,
|
|
400
|
+
});
|
|
217
401
|
const taskRuntime = new TaskRuntime({
|
|
218
402
|
commands: remoteCommands,
|
|
219
403
|
executeCommand: remoteEngine.executeCommand,
|
|
404
|
+
// A task's input, judged as its call's is before the task is recorded (`spec/tasks.md`).
|
|
405
|
+
validateCommand: (name, command, input, context) => checkTaskInput({ validated: serverValidated, maxValidationIssues, devMode, onError }, name, command, input, context),
|
|
220
406
|
store: options.taskStore,
|
|
221
|
-
options: options.taskOptions,
|
|
407
|
+
options: { ...options.taskOptions, allowInput: allowTaskInput },
|
|
222
408
|
clock,
|
|
223
|
-
hasAuth: Boolean(auth),
|
|
409
|
+
hasAuth: Boolean(options.auth),
|
|
224
410
|
hasContextHook: Boolean(options.createContext),
|
|
225
411
|
actor: options.actor ?? { kind: 'agent' },
|
|
412
|
+
confirmations: confirmations.mode,
|
|
413
|
+
humanRelay: options.humanRelay,
|
|
414
|
+
humanRelayGate: humanRelay,
|
|
415
|
+
approver: options.approver,
|
|
416
|
+
contextFields,
|
|
417
|
+
listedApprovalCommands,
|
|
418
|
+
devMode,
|
|
419
|
+
onError,
|
|
226
420
|
});
|
|
227
421
|
const modernDeps = {
|
|
228
422
|
name,
|
|
@@ -237,20 +431,80 @@ function createSharedHttpRuntime(options) {
|
|
|
237
431
|
confirmationKey: instanceState.confirmationKey,
|
|
238
432
|
actor: options.actor ?? { kind: 'agent' },
|
|
239
433
|
approver: options.approver,
|
|
434
|
+
identifiesHttpCaller,
|
|
240
435
|
resources,
|
|
241
436
|
resourcesTtlMs,
|
|
242
437
|
tasks: taskRuntime,
|
|
243
|
-
authConfigured: Boolean(auth),
|
|
438
|
+
authConfigured: Boolean(options.auth),
|
|
439
|
+
hasApprovals: middleware.some((mw) => approvalProcessState(mw) !== undefined),
|
|
440
|
+
hasConfirmations: confirmations.mode !== 'off' || Boolean(options.humanRelay),
|
|
441
|
+
hasIdempotency: idempotency !== undefined,
|
|
442
|
+
hasJournal: middleware.some((mw) => isJournalMiddleware(mw)),
|
|
244
443
|
devMode,
|
|
245
444
|
onError,
|
|
246
445
|
};
|
|
446
|
+
// The `/mcp` lane's options, then the handler's (its SSE hub's among them), as HTTP builds
|
|
447
|
+
// them: a stdio server builds neither, yet refuses what an HTTP one would. Stdio reads the TTL.
|
|
448
|
+
resolveToolsListTtlMs(modernDeps);
|
|
449
|
+
checkModernBodyLimit(options.maxBodyBytes ?? 1024 * 1024);
|
|
450
|
+
checkHttpHandlerOptions({
|
|
451
|
+
allowedOrigins: options.allowedOrigins,
|
|
452
|
+
maxBodyBytes: options.maxBodyBytes,
|
|
453
|
+
maxSseConnections: options.maxSseConnections,
|
|
454
|
+
});
|
|
455
|
+
return {
|
|
456
|
+
engine,
|
|
457
|
+
identifiesHttpCaller,
|
|
458
|
+
contextFields,
|
|
459
|
+
routeToolCall,
|
|
460
|
+
httpRouting,
|
|
461
|
+
taskRuntime,
|
|
462
|
+
getToolsList: boundGetToolsList,
|
|
463
|
+
modernDeps,
|
|
464
|
+
contextState,
|
|
465
|
+
remoteCommands,
|
|
466
|
+
httpWarning: instanceState.httpWarning,
|
|
467
|
+
url: `http://${host}:${port}`,
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
/** The HTTP session store's options: sessions exist only with `contexts`. */
|
|
471
|
+
function sessionStoreOptions(options) {
|
|
472
|
+
const { clock } = options;
|
|
473
|
+
return {
|
|
474
|
+
maxSessions: options.maxSessions,
|
|
475
|
+
// Without `auth` every caller is one owner, so its quota is the whole store.
|
|
476
|
+
maxSessionsPerOwner: options.auth
|
|
477
|
+
? options.maxSessionsPerOwner
|
|
478
|
+
: (options.maxSessions ?? DEFAULT_MAX_SESSIONS),
|
|
479
|
+
idleTimeoutMs: options.sessionIdleTimeoutMs,
|
|
480
|
+
...(clock ? { now: () => clock.now() } : {}),
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* What only HTTP serves, on a server's {@link createServerCore}: the session store, the
|
|
485
|
+
* authorization layer, the `/mcp` lane, and the handler with its SSE hub.
|
|
486
|
+
*/
|
|
487
|
+
function createHttpRuntime(core, options) {
|
|
488
|
+
const { name, version, port = 3100, host = 'localhost', devMode = false, cors = false, onError, toolStrategy = 'individual', groupByFn, maxBatchSize, maxBatchParallelism, clock, } = options;
|
|
489
|
+
const sessions = core.contextState ? createSessionStore(sessionStoreOptions(options)) : undefined;
|
|
490
|
+
const maxBodyBytes = options.maxBodyBytes ?? 1024 * 1024;
|
|
491
|
+
const auth = options.auth
|
|
492
|
+
? createAuthLayer(options.auth, {
|
|
493
|
+
maxBodyBytes,
|
|
494
|
+
clock,
|
|
495
|
+
commands: core.remoteCommands,
|
|
496
|
+
toolStrategy,
|
|
497
|
+
groupByFn,
|
|
498
|
+
})
|
|
499
|
+
: undefined;
|
|
247
500
|
const modern = createModernHttpLane({
|
|
248
|
-
...modernDeps,
|
|
501
|
+
...core.modernDeps,
|
|
249
502
|
createContext: options.createContext,
|
|
250
503
|
maxBodyBytes,
|
|
251
504
|
auth,
|
|
252
505
|
});
|
|
253
|
-
const {
|
|
506
|
+
const { httpEngine, restRouter, mutationNames } = core.httpRouting();
|
|
507
|
+
const { handler, dispose } = createHttpHandler({
|
|
254
508
|
name,
|
|
255
509
|
version,
|
|
256
510
|
host,
|
|
@@ -261,41 +515,50 @@ function createSharedHttpRuntime(options) {
|
|
|
261
515
|
allowedOrigins: options.allowedOrigins,
|
|
262
516
|
maxBodyBytes: options.maxBodyBytes,
|
|
263
517
|
maxSseConnections: options.maxSseConnections,
|
|
264
|
-
getToolsList:
|
|
265
|
-
routeToolCall,
|
|
518
|
+
getToolsList: core.getToolsList,
|
|
519
|
+
routeToolCall: core.routeToolCall,
|
|
266
520
|
approver: options.approver,
|
|
521
|
+
identifiesHttpCaller: core.identifiesHttpCaller,
|
|
267
522
|
actor: options.actor ?? { kind: 'agent' },
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
executeStream: remoteEngine.executeStream,
|
|
523
|
+
restRouter,
|
|
524
|
+
executeStream: httpEngine.executeStream,
|
|
271
525
|
isMutation: (commandName) => mutationNames.has(commandName),
|
|
272
|
-
confirmations,
|
|
273
526
|
sessions,
|
|
274
527
|
createContext: options.createContext,
|
|
275
|
-
contextFields,
|
|
528
|
+
contextFields: core.contextFields,
|
|
276
529
|
onError,
|
|
277
530
|
modern,
|
|
278
531
|
auth,
|
|
279
|
-
tasks: taskRuntime,
|
|
532
|
+
tasks: core.taskRuntime,
|
|
533
|
+
contract: serverContractFor(core.modernDeps, true),
|
|
280
534
|
maxBatchSize,
|
|
281
535
|
maxBatchParallelism,
|
|
282
536
|
});
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
537
|
+
return { handler, dispose, auth };
|
|
538
|
+
}
|
|
539
|
+
/**
|
|
540
|
+
* The wiring every lane of one server shares, with its HTTP runtime built: engines, routers,
|
|
541
|
+
* admission, the HTTP handler and the stdio lane's dependencies. Exported for tests that drive
|
|
542
|
+
* several lanes of one server; not part of the package's entry points.
|
|
543
|
+
*/
|
|
544
|
+
export function createSharedHttpRuntime(options) {
|
|
545
|
+
const core = createServerCore(options);
|
|
546
|
+
const http = createHttpRuntime(core, options);
|
|
287
547
|
return {
|
|
288
|
-
engine,
|
|
289
|
-
contextFields,
|
|
290
|
-
routeToolCall,
|
|
291
|
-
getToolsList:
|
|
292
|
-
handler,
|
|
293
|
-
dispose
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
548
|
+
engine: core.engine,
|
|
549
|
+
contextFields: core.contextFields,
|
|
550
|
+
routeToolCall: core.routeToolCall,
|
|
551
|
+
getToolsList: core.getToolsList,
|
|
552
|
+
handler: http.handler,
|
|
553
|
+
dispose: () => {
|
|
554
|
+
core.taskRuntime.dispose();
|
|
555
|
+
http.dispose();
|
|
556
|
+
},
|
|
557
|
+
modernDeps: core.modernDeps,
|
|
558
|
+
contextState: core.contextState,
|
|
559
|
+
auth: http.auth,
|
|
560
|
+
httpWarning: core.httpWarning,
|
|
561
|
+
url: core.url,
|
|
299
562
|
};
|
|
300
563
|
}
|
|
301
564
|
/**
|
|
@@ -342,7 +605,7 @@ export function createMcpHandler(options) {
|
|
|
342
605
|
* ```
|
|
343
606
|
*/
|
|
344
607
|
export function createMcpServer(options) {
|
|
345
|
-
const { name, version, commands, port = 3100, host = 'localhost', devMode = false, cors = false, transport = 'auto', middleware = [], onCommand, onError, toolStrategy = '
|
|
608
|
+
const { name, version, commands, port = 3100, host = 'localhost', devMode = false, cors = false, transport = 'auto', middleware = [], onCommand, onError, toolStrategy = 'individual', groupByFn, contexts, } = options;
|
|
346
609
|
// ── Transport resolution ────────────────────────────────────────────────
|
|
347
610
|
// Removed in 2.0. Fail loudly: silently ignoring it would switch `stdio: false` servers to
|
|
348
611
|
// auto-detection, which picks stdio whenever stdin is piped.
|
|
@@ -360,7 +623,7 @@ export function createMcpServer(options) {
|
|
|
360
623
|
const useHttp = resolvedTransport === 'http';
|
|
361
624
|
const autoDetected = transport === 'auto';
|
|
362
625
|
// ── Shared execution/runtime wiring ─────────────────────────────────────
|
|
363
|
-
const
|
|
626
|
+
const runtimeOptions = {
|
|
364
627
|
...options,
|
|
365
628
|
name,
|
|
366
629
|
version,
|
|
@@ -375,7 +638,14 @@ export function createMcpServer(options) {
|
|
|
375
638
|
toolStrategy,
|
|
376
639
|
groupByFn,
|
|
377
640
|
contexts,
|
|
378
|
-
}
|
|
641
|
+
};
|
|
642
|
+
const core = createServerCore(runtimeOptions);
|
|
643
|
+
// Only HTTP builds the HTTP runtime: stdio serves no session, SSE stream or token (#1746).
|
|
644
|
+
const http = useHttp ? createHttpRuntime(core, runtimeOptions) : undefined;
|
|
645
|
+
const dispose = () => {
|
|
646
|
+
core.taskRuntime.dispose();
|
|
647
|
+
http?.dispose();
|
|
648
|
+
};
|
|
379
649
|
// ── Server state ────────────────────────────────────────────────────────
|
|
380
650
|
let httpServer = null;
|
|
381
651
|
let stdioHandle = null;
|
|
@@ -387,7 +657,7 @@ export function createMcpServer(options) {
|
|
|
387
657
|
return;
|
|
388
658
|
// Stdio via the MCP SDK v2: dual-era (2026-07-28 per-request _meta, or initialize).
|
|
389
659
|
if (useStdio) {
|
|
390
|
-
stdioHandle = serveModernStdio(
|
|
660
|
+
stdioHandle = serveModernStdio(core.modernDeps, core.contextState);
|
|
391
661
|
isRunning = true;
|
|
392
662
|
// stdout carries MCP frames, so the notice goes to stderr.
|
|
393
663
|
console.error(autoDetected
|
|
@@ -397,8 +667,8 @@ export function createMcpServer(options) {
|
|
|
397
667
|
return;
|
|
398
668
|
}
|
|
399
669
|
// HTTP transport
|
|
400
|
-
if (
|
|
401
|
-
httpServer = createServer(
|
|
670
|
+
if (http) {
|
|
671
|
+
httpServer = createServer(http.handler);
|
|
402
672
|
await new Promise((resolve, reject) => {
|
|
403
673
|
httpServer?.on('error', reject);
|
|
404
674
|
httpServer?.listen(port, host, () => {
|
|
@@ -406,20 +676,20 @@ export function createMcpServer(options) {
|
|
|
406
676
|
resolve();
|
|
407
677
|
});
|
|
408
678
|
});
|
|
409
|
-
console.error(`[${name}] MCP transport: http at ${
|
|
410
|
-
warnInstancesOnce(
|
|
679
|
+
console.error(`[${name}] MCP transport: http at ${core.url}${autoDetected ? ' (auto-detected because stdin is a TTY)' : ''}`);
|
|
680
|
+
warnInstancesOnce(core.httpWarning);
|
|
411
681
|
}
|
|
412
682
|
},
|
|
413
683
|
async stop() {
|
|
414
684
|
if (!isRunning)
|
|
415
685
|
return;
|
|
416
686
|
if (stdioHandle) {
|
|
417
|
-
|
|
687
|
+
dispose();
|
|
418
688
|
await stdioHandle.close();
|
|
419
689
|
stdioHandle = null;
|
|
420
690
|
}
|
|
421
691
|
if (httpServer) {
|
|
422
|
-
|
|
692
|
+
dispose();
|
|
423
693
|
httpServer.closeAllConnections();
|
|
424
694
|
await new Promise((resolve, reject) => {
|
|
425
695
|
httpServer?.close((err) => {
|
|
@@ -434,9 +704,9 @@ export function createMcpServer(options) {
|
|
|
434
704
|
isRunning = false;
|
|
435
705
|
},
|
|
436
706
|
getUrl() {
|
|
437
|
-
return useHttp ?
|
|
707
|
+
return useHttp ? core.url : 'stdio://';
|
|
438
708
|
},
|
|
439
|
-
contextFields:
|
|
709
|
+
contextFields: core.contextFields,
|
|
440
710
|
getCommands() {
|
|
441
711
|
return commands;
|
|
442
712
|
},
|
|
@@ -446,10 +716,10 @@ export function createMcpServer(options) {
|
|
|
446
716
|
// An app's call needs a grant check this server does not have (`createAppCallRegistry`).
|
|
447
717
|
execute: (name, input, context) => context?.interface === 'apps'
|
|
448
718
|
? Promise.resolve(failure(callNotGranted()))
|
|
449
|
-
:
|
|
719
|
+
: core.engine.executeCommand(name, input, context),
|
|
450
720
|
executePipeline: (request, context) => context?.interface === 'apps'
|
|
451
721
|
? executeCorePipeline(request, async () => failure(callNotGranted()))
|
|
452
|
-
:
|
|
722
|
+
: core.engine.executePipeline(request, context),
|
|
453
723
|
};
|
|
454
724
|
}
|
|
455
725
|
//# sourceMappingURL=server.js.map
|