@softure-ai/mcp-access 0.2.9 → 0.4.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/CHANGELOG.md +66 -0
- package/README.md +241 -11
- package/dist/cli/run.d.ts +1 -1
- package/dist/cli/run.d.ts.map +1 -1
- package/dist/cli/run.js +2 -2
- package/dist/cli/run.js.map +1 -1
- package/dist/contract.d.ts +48 -2
- package/dist/contract.d.ts.map +1 -1
- package/dist/index.d.ts +88 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +19 -4
- package/dist/index.js.map +1 -1
- package/dist/jobs/index.d.ts +2 -0
- package/dist/jobs/index.d.ts.map +1 -1
- package/dist/jobs/index.js +5 -2
- package/dist/jobs/index.js.map +1 -1
- package/dist/jobs/operation-state.d.ts +58 -0
- package/dist/jobs/operation-state.d.ts.map +1 -0
- package/dist/jobs/operation-state.js +68 -0
- package/dist/jobs/operation-state.js.map +1 -0
- package/dist/jobs/operation-tasks.d.ts +68 -0
- package/dist/jobs/operation-tasks.d.ts.map +1 -0
- package/dist/jobs/operation-tasks.js +163 -0
- package/dist/jobs/operation-tasks.js.map +1 -0
- package/dist/jobs/operations.d.ts +89 -0
- package/dist/jobs/operations.d.ts.map +1 -0
- package/dist/jobs/operations.js +131 -0
- package/dist/jobs/operations.js.map +1 -0
- package/dist/messages/en.d.ts +55 -0
- package/dist/messages/en.d.ts.map +1 -1
- package/dist/messages/en.js +55 -0
- package/dist/messages/en.js.map +1 -1
- package/dist/messages/index.d.ts +54 -0
- package/dist/messages/index.d.ts.map +1 -1
- package/dist/next/issue-token.d.ts +11 -2
- package/dist/next/issue-token.d.ts.map +1 -1
- package/dist/next/issue-token.js +56 -7
- package/dist/next/issue-token.js.map +1 -1
- package/dist/next/token-manager-props.d.ts +3 -2
- package/dist/next/token-manager-props.d.ts.map +1 -1
- package/dist/next/token-manager-props.js +12 -2
- package/dist/next/token-manager-props.js.map +1 -1
- package/dist/options.d.ts +47 -0
- package/dist/options.d.ts.map +1 -1
- package/dist/options.js +53 -0
- package/dist/options.js.map +1 -1
- package/dist/schema.d.ts +214 -0
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +21 -3
- package/dist/schema.js.map +1 -1
- package/dist/server/api-tokens.d.ts +64 -0
- package/dist/server/api-tokens.d.ts.map +1 -0
- package/dist/server/api-tokens.js +176 -0
- package/dist/server/api-tokens.js.map +1 -0
- package/dist/server/confirmation.d.ts +69 -0
- package/dist/server/confirmation.d.ts.map +1 -0
- package/dist/server/confirmation.js +220 -0
- package/dist/server/confirmation.js.map +1 -0
- package/dist/server/health.d.ts.map +1 -1
- package/dist/server/health.js +3 -1
- package/dist/server/health.js.map +1 -1
- package/dist/server/index.d.ts +4 -2
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +3 -1
- package/dist/server/index.js.map +1 -1
- package/dist/server/privacy.d.ts +4 -0
- package/dist/server/privacy.d.ts.map +1 -1
- package/dist/server/privacy.js +5 -1
- package/dist/server/privacy.js.map +1 -1
- package/dist/server/prune.d.ts +5 -1
- package/dist/server/prune.d.ts.map +1 -1
- package/dist/server/prune.js +14 -4
- package/dist/server/prune.js.map +1 -1
- package/dist/server/strict-input.d.ts +3 -1
- package/dist/server/strict-input.d.ts.map +1 -1
- package/dist/server/strict-input.js +54 -8
- package/dist/server/strict-input.js.map +1 -1
- package/dist/server/tokens.d.ts +46 -11
- package/dist/server/tokens.d.ts.map +1 -1
- package/dist/server/tokens.js +46 -24
- package/dist/server/tokens.js.map +1 -1
- package/dist/server/tools.d.ts +8 -0
- package/dist/server/tools.d.ts.map +1 -1
- package/dist/server/tools.js +1 -0
- package/dist/server/tools.js.map +1 -1
- package/dist/tokens/index.d.ts +6 -0
- package/dist/tokens/index.d.ts.map +1 -0
- package/dist/tokens/index.js +8 -0
- package/dist/tokens/index.js.map +1 -0
- package/dist/ui/index.d.ts +1 -1
- package/dist/ui/index.d.ts.map +1 -1
- package/dist/ui/index.js.map +1 -1
- package/dist/ui/token-manager.d.ts +18 -2
- package/dist/ui/token-manager.d.ts.map +1 -1
- package/dist/ui/token-manager.js +28 -3
- package/dist/ui/token-manager.js.map +1 -1
- package/migrations/0004_create_used_confirmations.sql +13 -0
- package/migrations/0005_add_api_tokens.sql +15 -0
- package/migrations/0006_create_operation_tasks.sql +18 -0
- package/module.json +9 -3
- package/package.json +7 -2
- package/src/cli/run.ts +2 -2
- package/src/contract.ts +60 -2
- package/src/index.ts +31 -3
- package/src/jobs/index.ts +43 -2
- package/src/jobs/operation-state.ts +119 -0
- package/src/jobs/operation-tasks.ts +234 -0
- package/src/jobs/operations.ts +258 -0
- package/src/messages/en.ts +56 -0
- package/src/next/issue-token.ts +64 -9
- package/src/next/token-manager-props.ts +15 -4
- package/src/options.ts +62 -0
- package/src/schema.ts +23 -3
- package/src/server/api-tokens.ts +224 -0
- package/src/server/confirmation.ts +316 -0
- package/src/server/health.ts +3 -1
- package/src/server/index.ts +32 -2
- package/src/server/privacy.ts +9 -1
- package/src/server/prune.ts +17 -4
- package/src/server/strict-input.ts +62 -13
- package/src/server/tokens.ts +73 -27
- package/src/server/tools.ts +9 -0
- package/src/tokens/index.ts +22 -0
- package/src/ui/index.ts +1 -0
- package/src/ui/token-manager.tsx +140 -3
package/dist/index.d.ts
CHANGED
|
@@ -27,6 +27,23 @@ export declare const MCP_RATE_LIMIT_BUCKETS: {
|
|
|
27
27
|
readonly key: "ip";
|
|
28
28
|
};
|
|
29
29
|
};
|
|
30
|
+
/**
|
|
31
|
+
* The rate limit buckets of `requireApiToken`, to spread into `security({ buckets })` when the app sets
|
|
32
|
+
* `apiTokens`: `api` counts requests per client address before the token is looked up (guessing),
|
|
33
|
+
* `api-token` counts requests per verified token (one script or Shortcut cannot flood a route).
|
|
34
|
+
*/
|
|
35
|
+
export declare const API_TOKEN_RATE_LIMIT_BUCKETS: {
|
|
36
|
+
readonly api: {
|
|
37
|
+
readonly limit: 300;
|
|
38
|
+
readonly windowMinutes: 15;
|
|
39
|
+
readonly key: "ip";
|
|
40
|
+
};
|
|
41
|
+
readonly "api-token": {
|
|
42
|
+
readonly limit: 120;
|
|
43
|
+
readonly windowMinutes: 15;
|
|
44
|
+
readonly key: "subject";
|
|
45
|
+
};
|
|
46
|
+
};
|
|
30
47
|
/**
|
|
31
48
|
* Enables MCP access in `softure.config.ts` (after `security` and `auth`):
|
|
32
49
|
* `mcpAccess({ serverName: "acme", tools: [{ name: "list_orders", access: "read", description: { en: "Lists your orders." } }] })`.
|
|
@@ -60,6 +77,28 @@ export declare const mcpAccess: import("@softure-ai/core").ModuleFactory<{
|
|
|
60
77
|
pending: string;
|
|
61
78
|
limit: string;
|
|
62
79
|
};
|
|
80
|
+
apiScopes: {
|
|
81
|
+
title: string;
|
|
82
|
+
lead: string;
|
|
83
|
+
read: string;
|
|
84
|
+
write: string;
|
|
85
|
+
writeUnavailable: string;
|
|
86
|
+
};
|
|
87
|
+
apiIssue: {
|
|
88
|
+
title: string;
|
|
89
|
+
name: string;
|
|
90
|
+
nameHint: string;
|
|
91
|
+
scopes: string;
|
|
92
|
+
submit: string;
|
|
93
|
+
pending: string;
|
|
94
|
+
};
|
|
95
|
+
apiIssued: {
|
|
96
|
+
title: string;
|
|
97
|
+
once: string;
|
|
98
|
+
scopes: string;
|
|
99
|
+
tokenTitle: string;
|
|
100
|
+
headerTitle: string;
|
|
101
|
+
};
|
|
63
102
|
issued: {
|
|
64
103
|
title: string;
|
|
65
104
|
once: string;
|
|
@@ -82,6 +121,8 @@ export declare const mcpAccess: import("@softure-ai/core").ModuleFactory<{
|
|
|
82
121
|
};
|
|
83
122
|
list: {
|
|
84
123
|
title: string;
|
|
124
|
+
kindMcp: string;
|
|
125
|
+
kindApi: string;
|
|
85
126
|
empty: string;
|
|
86
127
|
readOnly: string;
|
|
87
128
|
readWrite: string;
|
|
@@ -131,9 +172,36 @@ export declare const mcpAccess: import("@softure-ai/core").ModuleFactory<{
|
|
|
131
172
|
noFields: string;
|
|
132
173
|
fieldRenamed: string;
|
|
133
174
|
};
|
|
175
|
+
confirmation: {
|
|
176
|
+
ask: string;
|
|
177
|
+
fieldDescription: string;
|
|
178
|
+
invalid: string;
|
|
179
|
+
otherTool: string;
|
|
180
|
+
otherAccount: string;
|
|
181
|
+
expired: string;
|
|
182
|
+
argumentsChanged: string;
|
|
183
|
+
estimateChanged: string;
|
|
184
|
+
used: string;
|
|
185
|
+
};
|
|
134
186
|
serverInfoTool: {
|
|
135
187
|
description: string;
|
|
136
188
|
};
|
|
189
|
+
operations: {
|
|
190
|
+
statusToolDescription: string;
|
|
191
|
+
cancelToolDescription: string;
|
|
192
|
+
started: string;
|
|
193
|
+
queued: string;
|
|
194
|
+
running: string;
|
|
195
|
+
cancelRequested: string;
|
|
196
|
+
succeeded: string;
|
|
197
|
+
failed: string;
|
|
198
|
+
cancelled: string;
|
|
199
|
+
notFound: string;
|
|
200
|
+
finished: string;
|
|
201
|
+
taskQueued: string;
|
|
202
|
+
taskRunning: string;
|
|
203
|
+
taskCancelRequested: string;
|
|
204
|
+
};
|
|
137
205
|
errors: {
|
|
138
206
|
"mcp-access": {
|
|
139
207
|
name_required: string;
|
|
@@ -143,6 +211,9 @@ export declare const mcpAccess: import("@softure-ai/core").ModuleFactory<{
|
|
|
143
211
|
grant_not_found: string;
|
|
144
212
|
issue_refused: string;
|
|
145
213
|
write_refused: string;
|
|
214
|
+
api_disabled: string;
|
|
215
|
+
scope_required: string;
|
|
216
|
+
scope_unknown: string;
|
|
146
217
|
};
|
|
147
218
|
auth: {
|
|
148
219
|
unauthenticated: string;
|
|
@@ -187,11 +258,26 @@ export declare const mcpAccess: import("@softure-ai/core").ModuleFactory<{
|
|
|
187
258
|
protectedResource: import("zod").ZodOptional<import("zod").ZodUnion<readonly [import("zod").ZodCustom<(origins: import("./origins.js").McpOrigins) => Readonly<Record<string, unknown>>, (origins: import("./origins.js").McpOrigins) => Readonly<Record<string, unknown>>>, import("zod").ZodRecord<import("zod").ZodString, import("zod").ZodUnknown>]>>;
|
|
188
259
|
}, import("zod/v4/core").$strict>>;
|
|
189
260
|
}, import("zod/v4/core").$strict>>;
|
|
261
|
+
apiTokens: import("zod").ZodOptional<import("zod").ZodObject<{
|
|
262
|
+
prefix: import("zod").ZodDefault<import("zod").ZodString>;
|
|
263
|
+
scopes: import("zod").ZodArray<import("zod").ZodObject<{
|
|
264
|
+
name: import("zod").ZodString;
|
|
265
|
+
access: import("zod").ZodEnum<{
|
|
266
|
+
read: "read";
|
|
267
|
+
write: "write";
|
|
268
|
+
}>;
|
|
269
|
+
description: import("zod").ZodRecord<import("zod").ZodString & import("zod").ZodType<string, string, import("zod/v4/core").$ZodTypeInternals<string, string>>, import("zod").ZodString>;
|
|
270
|
+
}, import("zod/v4/core").$strict>>;
|
|
271
|
+
examples: import("zod").ZodDefault<import("zod").ZodArray<import("zod").ZodObject<{
|
|
272
|
+
title: import("zod").ZodRecord<import("zod").ZodString & import("zod").ZodType<string, string, import("zod/v4/core").$ZodTypeInternals<string, string>>, import("zod").ZodString>;
|
|
273
|
+
text: import("zod").ZodRecord<import("zod").ZodString & import("zod").ZodType<string, string, import("zod/v4/core").$ZodTypeInternals<string, string>>, import("zod").ZodString>;
|
|
274
|
+
}, import("zod/v4/core").$strict>>>;
|
|
275
|
+
}, import("zod/v4/core").$strict>>;
|
|
190
276
|
}, import("zod/v4/core").$strict>>;
|
|
191
277
|
export { getAuthorizationHeader, getClaudeCodeCommand, getClaudeCodeLink, getDesktopConfig, getJsonConfig, getMcpClientSetup, type McpClientSetupInput, } from "./client-setup.js";
|
|
192
|
-
export type { AccessTokenStatus, AccessTokenView, IssuedToken, IssueTokenFormState, McpAccessErrorCode, McpIssueRefusal, McpIssueRefusalCode, McpClientSetup, McpServerIdentity, OAuthGrantView, RevokeGrantFormState, RevokeTokenFormState, TokenFormErrorCode, } from "./contract.js";
|
|
278
|
+
export type { AccessTokenKind, AccessTokenStatus, AccessTokenView, ApiTokenErrorCode, ApiTokenExample, ApiTokenIdentity, ApiTokenRefusal, IssuedApiTokenView, IssuedToken, IssueTokenFormState, McpAccessErrorCode, McpIssueRefusal, McpIssueRefusalCode, McpClientSetup, McpServerIdentity, OAuthGrantView, RevokeGrantFormState, RevokeTokenFormState, TokenFormErrorCode, } from "./contract.js";
|
|
193
279
|
export { getTokenErrorMessage, mcpAccessMessages, type McpAccessMessages } from "./messages/index.js";
|
|
194
|
-
export { MAX_PRESENTED_TOKEN_LENGTH, MAX_TOKEN_NAME_LENGTH, SERVER_NAME_PATTERN, TOOL_NAME_PATTERN, GENERATED_AUTHORIZATION_SERVER_KEYS, GENERATED_PROTECTED_RESOURCE_KEYS, type McpAccessOptions, type McpAccessOptionsInput, type McpAppOriginResolver, type McpMetadataExtension, type McpOAuthOptions, type McpToolAccess, type McpToolDefinition, type McpToolDefinitionInput, } from "./options.js";
|
|
280
|
+
export { API_SCOPE_PATTERN, API_TOKEN_PREFIX_PATTERN, MAX_API_TOKEN_SCOPES, MAX_PRESENTED_TOKEN_LENGTH, MAX_TOKEN_NAME_LENGTH, SERVER_NAME_PATTERN, TOOL_NAME_PATTERN, GENERATED_AUTHORIZATION_SERVER_KEYS, GENERATED_PROTECTED_RESOURCE_KEYS, type ApiScopeDefinition, type ApiTokenOptions, type ApiTokenOptionsInput, type McpAccessOptions, type McpAccessOptionsInput, type McpAppOriginResolver, type McpMetadataExtension, type McpOAuthOptions, type McpToolAccess, type McpToolDefinition, type McpToolDefinitionInput, } from "./options.js";
|
|
195
281
|
export { DEFAULT_MCP_ACCESS_ROUTES } from "./routes.js";
|
|
196
282
|
export { readRequestOrigin, type McpOriginRequest, type McpOrigins } from "./origins.js";
|
|
197
283
|
export { accessTokens, oauthAuthorizationCodes, oauthClients, oauthGrants, type OAuthClientRow, type OAuthTokenEndpointAuthMethod } from "./schema.js";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAY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oB,EACpB,kBAAkB,GACnB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,KAAK,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACtG,OAAO,EACL,iBAAiB,EACjB,wBAAwB,EACxB,oBAAoB,EACpB,0BAA0B,EAC1B,qBAAqB,EACrB,mBAAmB,EACnB,iBAAiB,EACjB,mCAAmC,EACnC,iCAAiC,EACjC,KAAK,kBAAkB,EACvB,KAAK,eAAe,EACpB,KAAK,oBAAoB,EACzB,KAAK,gBAAgB,EACrB,KAAK,qBAAqB,EAC1B,KAAK,oBAAoB,EACzB,KAAK,oBAAoB,EACzB,KAAK,eAAe,EACpB,KAAK,aAAa,EAClB,KAAK,iBAAiB,EACtB,KAAK,sBAAsB,GAC5B,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,yBAAyB,EAAE,MAAM,aAAa,CAAC;AACxD,OAAO,EAAE,iBAAiB,EAAE,KAAK,gBAAgB,EAAE,KAAK,UAAU,EAAE,MAAM,cAAc,CAAC;AACzF,OAAO,EAAE,YAAY,EAAE,uBAAuB,EAAE,YAAY,EAAE,WAAW,EAAE,KAAK,cAAc,EAAE,KAAK,4BAA4B,EAAE,MAAM,aAAa,CAAC;AACvJ,OAAO,EAAE,oBAAoB,EAAE,KAAK,wBAAwB,EAAE,MAAM,mBAAmB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -29,6 +29,15 @@ export const MCP_RATE_LIMIT_BUCKETS = {
|
|
|
29
29
|
*/
|
|
30
30
|
"mcp-oauth": { limit: 60, windowMinutes: 15, key: "ip" },
|
|
31
31
|
};
|
|
32
|
+
/**
|
|
33
|
+
* The rate limit buckets of `requireApiToken`, to spread into `security({ buckets })` when the app sets
|
|
34
|
+
* `apiTokens`: `api` counts requests per client address before the token is looked up (guessing),
|
|
35
|
+
* `api-token` counts requests per verified token (one script or Shortcut cannot flood a route).
|
|
36
|
+
*/
|
|
37
|
+
export const API_TOKEN_RATE_LIMIT_BUCKETS = {
|
|
38
|
+
api: { limit: 300, windowMinutes: 15, key: "ip" },
|
|
39
|
+
"api-token": { limit: 120, windowMinutes: 15, key: "subject" },
|
|
40
|
+
};
|
|
32
41
|
/**
|
|
33
42
|
* Enables MCP access in `softure.config.ts` (after `security` and `auth`):
|
|
34
43
|
* `mcpAccess({ serverName: "acme", tools: [{ name: "list_orders", access: "read", description: { en: "Lists your orders." } }] })`.
|
|
@@ -36,11 +45,17 @@ export const MCP_RATE_LIMIT_BUCKETS = {
|
|
|
36
45
|
export const mcpAccess = defineModule({
|
|
37
46
|
manifest: {
|
|
38
47
|
id: MODULE_ID,
|
|
39
|
-
version: "0.
|
|
48
|
+
version: "0.4.0",
|
|
40
49
|
dependsOn: { security: "^0.2.0", auth: "^0.2.0", jobs: "^0.1.1?", ops: "^0.2.0?" },
|
|
41
50
|
dbSchema: "mcp",
|
|
42
|
-
tables: ["access_tokens", "oauth_clients", "oauth_authorization_codes", "oauth_grants", "oauth_registration_window"],
|
|
43
|
-
env: [
|
|
51
|
+
tables: ["access_tokens", "oauth_clients", "oauth_authorization_codes", "oauth_grants", "oauth_registration_window", "used_confirmations", "operation_tasks"],
|
|
52
|
+
env: [
|
|
53
|
+
{
|
|
54
|
+
name: "MCP_CONFIRMATION_SECRET",
|
|
55
|
+
required: false,
|
|
56
|
+
description: "Signs and verifies the confirmation tokens of confirmed tool calls (withConfirmation), at least 32 characters; required only by an app that confirms tool calls. Changing it voids the tokens in flight.",
|
|
57
|
+
},
|
|
58
|
+
],
|
|
44
59
|
switches: [WRITES_ENABLED_SWITCH],
|
|
45
60
|
routes: { ...DEFAULT_MCP_ACCESS_ROUTES },
|
|
46
61
|
mount: [
|
|
@@ -64,7 +79,7 @@ export const mcpAccess = defineModule({
|
|
|
64
79
|
});
|
|
65
80
|
export { getAuthorizationHeader, getClaudeCodeCommand, getClaudeCodeLink, getDesktopConfig, getJsonConfig, getMcpClientSetup, } from "./client-setup.js";
|
|
66
81
|
export { getTokenErrorMessage, mcpAccessMessages } from "./messages/index.js";
|
|
67
|
-
export { MAX_PRESENTED_TOKEN_LENGTH, MAX_TOKEN_NAME_LENGTH, SERVER_NAME_PATTERN, TOOL_NAME_PATTERN, GENERATED_AUTHORIZATION_SERVER_KEYS, GENERATED_PROTECTED_RESOURCE_KEYS, } from "./options.js";
|
|
82
|
+
export { API_SCOPE_PATTERN, API_TOKEN_PREFIX_PATTERN, MAX_API_TOKEN_SCOPES, MAX_PRESENTED_TOKEN_LENGTH, MAX_TOKEN_NAME_LENGTH, SERVER_NAME_PATTERN, TOOL_NAME_PATTERN, GENERATED_AUTHORIZATION_SERVER_KEYS, GENERATED_PROTECTED_RESOURCE_KEYS, } from "./options.js";
|
|
68
83
|
export { DEFAULT_MCP_ACCESS_ROUTES } from "./routes.js";
|
|
69
84
|
export { readRequestOrigin } from "./origins.js";
|
|
70
85
|
export { accessTokens, oauthAuthorizationCodes, oauthClients, oauthGrants } from "./schema.js";
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,iGAAiG;AACjG,kGAAkG;AAClG,kGAAkG;AAClG,mDAAmD;AACnD,OAAO,EAAE,YAAY,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AAEtE,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAC;AACtD,OAAO,EAAE,yBAAyB,EAAE,MAAM,aAAa,CAAC;AACxD,OAAO,EAAE,sBAAsB,EAAE,MAAM,oBAAoB,CAAC;AAC5D,OAAO,EAAE,2BAA2B,EAAE,MAAM,qBAAqB,CAAC;AAElE,MAAM,CAAC,MAAM,SAAS,GAAG,YAAY,CAAC;AAEtC;;;;GAIG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,2BAA2B,CAAC;AAEjE;;;;GAIG;A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,EACpB,0BAA0B,EAC1B,qBAAqB,EACrB,mBAAmB,EACnB,iBAAiB,EACjB,mCAAmC,EACnC,iCAAiC,GAYlC,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,yBAAyB,EAAE,MAAM,aAAa,CAAC;AACxD,OAAO,EAAE,iBAAiB,EAA0C,MAAM,cAAc,CAAC;AACzF,OAAO,EAAE,YAAY,EAAE,uBAAuB,EAAE,YAAY,EAAE,WAAW,EAA0D,MAAM,aAAa,CAAC;AACvJ,OAAO,EAAE,oBAAoB,EAAiC,MAAM,mBAAmB,CAAC"}
|
package/dist/jobs/index.d.ts
CHANGED
|
@@ -9,4 +9,6 @@ export declare const mcpAccessJobSchedules: {
|
|
|
9
9
|
every: string;
|
|
10
10
|
};
|
|
11
11
|
};
|
|
12
|
+
export { cancelOperation, getOperation, getOperationErrorHints, getOperationStatus, getPollAfterSeconds, MAX_OPERATION_DEDUP_KEY_LENGTH, OPERATION_CANCEL_TOOL_NAME, OPERATION_ORIGIN, OPERATION_STATUS_TOOL_NAME, OPERATION_STATUSES, registerOperationCancelTool, registerOperationStatusTool, startOperation, type GetOperationOptions, type OperationAnswer, type OperationCancelToolOptions, type OperationErrorCode, type OperationJob, type OperationPollOptions, type OperationStatus, type OperationStatusToolOptions, type OperationToolContext, type ReadOperationResult, type StartOperationInput, } from "./operations.js";
|
|
13
|
+
export { DEFAULT_TASK_TTL_SECONDS, getOperationTask, isTasksRequest, MAX_TASK_TTL_SECONDS, MIN_TASK_TTL_SECONDS, registerOperationTasks, TASKS_EXTENSION, type OperationTask, type OperationTaskRequest, type OperationTasksOptions, type OperationTaskStatus, } from "./operation-tasks.js";
|
|
12
14
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/jobs/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/jobs/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/jobs/index.ts"],"names":[],"mappings":"AAQA,wEAAwE;AACxE,eAAO,MAAM,oBAAoB;IAC/B,+EAA+E;;CAQ1D,CAAC;AAExB,gFAAgF;AAChF,eAAO,MAAM,qBAAqB;;;;CAEL,CAAC;AAE9B,OAAO,EACL,eAAe,EACf,YAAY,EACZ,sBAAsB,EACtB,kBAAkB,EAClB,mBAAmB,EACnB,8BAA8B,EAC9B,0BAA0B,EAC1B,gBAAgB,EAChB,0BAA0B,EAC1B,kBAAkB,EAClB,2BAA2B,EAC3B,2BAA2B,EAC3B,cAAc,EACd,KAAK,mBAAmB,EACxB,KAAK,eAAe,EACpB,KAAK,0BAA0B,EAC/B,KAAK,kBAAkB,EACvB,KAAK,YAAY,EACjB,KAAK,oBAAoB,EACzB,KAAK,eAAe,EACpB,KAAK,0BAA0B,EAC/B,KAAK,oBAAoB,EACzB,KAAK,mBAAmB,EACxB,KAAK,mBAAmB,GACzB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACL,wBAAwB,EACxB,gBAAgB,EAChB,cAAc,EACd,oBAAoB,EACpB,oBAAoB,EACpB,sBAAsB,EACtB,eAAe,EACf,KAAK,aAAa,EAClB,KAAK,oBAAoB,EACzB,KAAK,qBAAqB,EAC1B,KAAK,mBAAmB,GACzB,MAAM,sBAAsB,CAAC"}
|
package/dist/jobs/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
// The jobs adapter of @softure-ai/mcp-access: its daily cleanup as a `@softure-ai/jobs` handler and schedule
|
|
2
|
-
//
|
|
1
|
+
// The jobs adapter of @softure-ai/mcp-access: its daily cleanup as a `@softure-ai/jobs` handler and schedule, and long
|
|
2
|
+
// operations as MCP tools (`startOperation`, `get_operation`, `cancel_operation`) or as MCP Tasks
|
|
3
|
+
// (`registerOperationTasks`). Jobs is an optional peer dependency; only apps that import this entry need it.
|
|
3
4
|
import { ok } from "@softure-ai/core";
|
|
4
5
|
import { defineJobHandler } from "@softure-ai/jobs";
|
|
5
6
|
import { z } from "zod";
|
|
@@ -19,4 +20,6 @@ export const mcpAccessJobHandlers = {
|
|
|
19
20
|
export const mcpAccessJobSchedules = {
|
|
20
21
|
"mcp-access.prune": { every: "1d" },
|
|
21
22
|
};
|
|
23
|
+
export { cancelOperation, getOperation, getOperationErrorHints, getOperationStatus, getPollAfterSeconds, MAX_OPERATION_DEDUP_KEY_LENGTH, OPERATION_CANCEL_TOOL_NAME, OPERATION_ORIGIN, OPERATION_STATUS_TOOL_NAME, OPERATION_STATUSES, registerOperationCancelTool, registerOperationStatusTool, startOperation, } from "./operations.js";
|
|
24
|
+
export { DEFAULT_TASK_TTL_SECONDS, getOperationTask, isTasksRequest, MAX_TASK_TTL_SECONDS, MIN_TASK_TTL_SECONDS, registerOperationTasks, TASKS_EXTENSION, } from "./operation-tasks.js";
|
|
22
25
|
//# sourceMappingURL=index.js.map
|
package/dist/jobs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/jobs/index.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/jobs/index.ts"],"names":[],"mappings":"AAAA,uHAAuH;AACvH,kGAAkG;AAClG,6GAA6G;AAC7G,OAAO,EAAE,EAAE,EAAE,MAAM,kBAAkB,CAAC;AACtC,OAAO,EAAE,gBAAgB,EAA4C,MAAM,kBAAkB,CAAC;AAC9F,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAEpD,wEAAwE;AACxE,MAAM,CAAC,MAAM,oBAAoB,GAAG;IAClC,+EAA+E;IAC/E,kBAAkB,EAAE,gBAAgB,CAAC;QACnC,OAAO,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,CAAC;QACrB,GAAG,EAAE,KAAK,EAAE,QAAQ,EAAE,GAAG,EAAE,EAAE;YAC3B,MAAM,cAAc,CAAC,GAAG,CAAC,CAAC;YAC1B,OAAO,EAAE,EAAE,CAAC;QACd,CAAC;KACF,CAAC;CACmB,CAAC;AAExB,gFAAgF;AAChF,MAAM,CAAC,MAAM,qBAAqB,GAAG;IACnC,kBAAkB,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE;CACR,CAAC;AAE9B,OAAO,EACL,eAAe,EACf,YAAY,EACZ,sBAAsB,EACtB,kBAAkB,EAClB,mBAAmB,EACnB,8BAA8B,EAC9B,0BAA0B,EAC1B,gBAAgB,EAChB,0BAA0B,EAC1B,kBAAkB,EAClB,2BAA2B,EAC3B,2BAA2B,EAC3B,cAAc,GAYf,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACL,wBAAwB,EACxB,gBAAgB,EAChB,cAAc,EACd,oBAAoB,EACpB,oBAAoB,EACpB,sBAAsB,EACtB,eAAe,GAKhB,MAAM,sBAAsB,CAAC"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { type Job, type JobProgress } from "@softure-ai/jobs";
|
|
2
|
+
import { type JobsContext } from "@softure-ai/jobs/server";
|
|
3
|
+
import type { McpServerIdentity } from "../contract.js";
|
|
4
|
+
/** The origin of every operation's job: the jobs admin page and run log show where it came from. */
|
|
5
|
+
export declare const OPERATION_ORIGIN = "mcp";
|
|
6
|
+
export declare const OPERATION_STATUS_TOOL_NAME = "get_operation";
|
|
7
|
+
export declare const OPERATION_CANCEL_TOOL_NAME = "cancel_operation";
|
|
8
|
+
/** The longest dedup key an app may pass: the column holds 200 characters and the owner's prefix takes 42. */
|
|
9
|
+
export declare const MAX_OPERATION_DEDUP_KEY_LENGTH = 150;
|
|
10
|
+
export declare const OPERATION_STATUSES: readonly ["queued", "running", "succeeded", "failed", "cancelled"];
|
|
11
|
+
/** An operation's status as the assistant reads it: a retrying job is `running`, since the operation has not ended. */
|
|
12
|
+
export type OperationStatus = (typeof OPERATION_STATUSES)[number];
|
|
13
|
+
export type OperationErrorCode = "mcp-access.operation_not_found" | "mcp-access.operation_finished";
|
|
14
|
+
/** How often the assistant is told to ask again: between `minSeconds` (default 2) and `maxSeconds` (default 30). */
|
|
15
|
+
export interface OperationPollOptions {
|
|
16
|
+
readonly minSeconds?: number;
|
|
17
|
+
readonly maxSeconds?: number;
|
|
18
|
+
}
|
|
19
|
+
/** The job behind an operation, as `readResult` receives it. */
|
|
20
|
+
export interface OperationJob {
|
|
21
|
+
readonly id: string;
|
|
22
|
+
readonly queue: string;
|
|
23
|
+
readonly payload: unknown;
|
|
24
|
+
readonly finishedAt: Date | null;
|
|
25
|
+
}
|
|
26
|
+
/** The app's result of a succeeded operation (a URL, an id, counts), read from its own tables; never a secret. */
|
|
27
|
+
export type ReadOperationResult = (job: OperationJob) => unknown;
|
|
28
|
+
/** What `get_operation` answers. `pollAfterSeconds` only while the operation runs, `result` and `errorCode` once ended. */
|
|
29
|
+
export interface OperationAnswer {
|
|
30
|
+
readonly operationId: string;
|
|
31
|
+
readonly status: OperationStatus;
|
|
32
|
+
/** The running attempt's progress (`context.reportProgress`): codes and counts only. */
|
|
33
|
+
readonly progress: JobProgress | null;
|
|
34
|
+
readonly pollAfterSeconds?: number;
|
|
35
|
+
/** True once someone asked a running operation to stop and it has not ended yet. */
|
|
36
|
+
readonly cancelRequested?: boolean;
|
|
37
|
+
readonly result?: unknown;
|
|
38
|
+
/** The failure's code (`lastError`), `jobs.cancelled` for a cancelled operation. */
|
|
39
|
+
readonly errorCode?: string;
|
|
40
|
+
/** What the assistant should do next, in a sentence. */
|
|
41
|
+
readonly message: string;
|
|
42
|
+
}
|
|
43
|
+
export interface GetOperationOptions {
|
|
44
|
+
readonly readResult?: ReadOperationResult;
|
|
45
|
+
readonly poll?: OperationPollOptions;
|
|
46
|
+
readonly statusToolName?: string;
|
|
47
|
+
}
|
|
48
|
+
/** The operation's status: a pending job that has used an attempt is waiting for a retry, so it is `running`. */
|
|
49
|
+
export declare function getOperationStatus(job: Pick<Job, "status" | "attempts">): OperationStatus;
|
|
50
|
+
/** Seconds until the next status call: a quarter of the operation's age, clamped to the poll bounds. */
|
|
51
|
+
export declare function getPollAfterSeconds(ageSeconds: number, poll?: OperationPollOptions): number;
|
|
52
|
+
export declare function describeOperation(ctx: JobsContext, job: Job, options: GetOperationOptions): Promise<OperationAnswer>;
|
|
53
|
+
/** The job behind the caller's operation, or null: not a job, not an operation, or another account's. */
|
|
54
|
+
export declare function findOwnedJob(ctx: JobsContext, identity: McpServerIdentity, operationId: string): Promise<Job | null>;
|
|
55
|
+
export declare function getOwnerPrefix(userId: string): string;
|
|
56
|
+
export declare function getPollMin(poll?: OperationPollOptions): number;
|
|
57
|
+
export declare function fillToolName(text: string, toolName: string): string;
|
|
58
|
+
//# sourceMappingURL=operation-state.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"operation-state.d.ts","sourceRoot":"","sources":["../../src/jobs/operation-state.ts"],"names":[],"mappings":"AAEA,OAAO,EAAsB,KAAK,GAAG,EAAE,KAAK,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAClF,OAAO,EAAU,KAAK,WAAW,EAAE,MAAM,yBAAyB,CAAC;AACnE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAGxD,oGAAoG;AACpG,eAAO,MAAM,gBAAgB,QAAQ,CAAC;AACtC,eAAO,MAAM,0BAA0B,kBAAkB,CAAC;AAC1D,eAAO,MAAM,0BAA0B,qBAAqB,CAAC;AAC7D,8GAA8G;AAC9G,eAAO,MAAM,8BAA8B,MAAM,CAAC;AAOlD,eAAO,MAAM,kBAAkB,oEAAqE,CAAC;AAErG,uHAAuH;AACvH,MAAM,MAAM,eAAe,GAAG,CAAC,OAAO,kBAAkB,CAAC,CAAC,MAAM,CAAC,CAAC;AAElE,MAAM,MAAM,kBAAkB,GAAG,gCAAgC,GAAG,+BAA+B,CAAC;AAEpG,oHAAoH;AACpH,MAAM,WAAW,oBAAoB;IACnC,QAAQ,CAAC,UAAU,CAAC,EA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,CAAC,UAAU,EAAE,MAAM,EAAE,IAAI,GAAE,oBAAyB,GAAG,MAAM,CAI/F;AAED,wBAAsB,iBAAiB,CAAC,GAAG,EAAE,WAAW,EAAE,GAAG,EAAE,GAAG,EAAE,OAAO,EAAE,mBAAmB,GAAG,OAAO,CAAC,eAAe,CAAC,CAqB1H;AAED,yGAAyG;AACzG,wBAAsB,YAAY,CAAC,GAAG,EAAE,WAAW,EAAE,QAAQ,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,GAAG,IAAI,CAAC,CAI1H;AAED,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAErD;AAED,wBAAgB,UAAU,CAAC,IAAI,GAAE,oBAAyB,GAAG,MAAM,CAElE;AAED,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAEnE"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// The state of a long operation (#952) as both answers read it: the polling tools (`get_operation`) and MCP Tasks
|
|
2
|
+
// (#977). An operation is a job of origin `mcp` whose dedup key starts with `mcp:<userId>:`.
|
|
3
|
+
import { JOB_CANCELLED_CODE } from "@softure-ai/jobs";
|
|
4
|
+
import { getJob } from "@softure-ai/jobs/server";
|
|
5
|
+
import { getMcpAccessMessages } from "../server/options.js";
|
|
6
|
+
/** The origin of every operation's job: the jobs admin page and run log show where it came from. */
|
|
7
|
+
export const OPERATION_ORIGIN = "mcp";
|
|
8
|
+
export const OPERATION_STATUS_TOOL_NAME = "get_operation";
|
|
9
|
+
export const OPERATION_CANCEL_TOOL_NAME = "cancel_operation";
|
|
10
|
+
/** The longest dedup key an app may pass: the column holds 200 characters and the owner's prefix takes 42. */
|
|
11
|
+
export const MAX_OPERATION_DEDUP_KEY_LENGTH = 150;
|
|
12
|
+
const DEFAULT_MIN_POLL_SECONDS = 2;
|
|
13
|
+
const DEFAULT_MAX_POLL_SECONDS = 30;
|
|
14
|
+
/** The wait grows with the operation's age: a quarter of the seconds since it started. */
|
|
15
|
+
const POLL_AGE_FACTOR = 0.25;
|
|
16
|
+
export const OPERATION_STATUSES = ["queued", "running", "succeeded", "failed", "cancelled"];
|
|
17
|
+
/** The operation's status: a pending job that has used an attempt is waiting for a retry, so it is `running`. */
|
|
18
|
+
export function getOperationStatus(job) {
|
|
19
|
+
if (job.status === "pending")
|
|
20
|
+
return job.attempts === 0 ? "queued" : "running";
|
|
21
|
+
return job.status;
|
|
22
|
+
}
|
|
23
|
+
/** Seconds until the next status call: a quarter of the operation's age, clamped to the poll bounds. */
|
|
24
|
+
export function getPollAfterSeconds(ageSeconds, poll = {}) {
|
|
25
|
+
const min = getPollMin(poll);
|
|
26
|
+
const max = Math.max(min, poll.maxSeconds ?? DEFAULT_MAX_POLL_SECONDS);
|
|
27
|
+
return Math.min(max, Math.max(min, Math.round(Math.max(0, ageSeconds) * POLL_AGE_FACTOR)));
|
|
28
|
+
}
|
|
29
|
+
export async function describeOperation(ctx, job, options) {
|
|
30
|
+
const messages = getMcpAccessMessages(ctx.config).operations;
|
|
31
|
+
const status = getOperationStatus(job);
|
|
32
|
+
const statusToolName = options.statusToolName ?? OPERATION_STATUS_TOOL_NAME;
|
|
33
|
+
const base = { operationId: job.id, status, progress: status === "running" ? job.progress : null };
|
|
34
|
+
if (status === "queued" || status === "running") {
|
|
35
|
+
const ageSeconds = (ctx.clock.now().getTime() - job.createdAt.getTime()) / 1000;
|
|
36
|
+
const cancelRequested = job.cancelRequestedAt !== null;
|
|
37
|
+
return {
|
|
38
|
+
...base,
|
|
39
|
+
pollAfterSeconds: getPollAfterSeconds(ageSeconds, options.poll),
|
|
40
|
+
...(cancelRequested ? { cancelRequested } : {}),
|
|
41
|
+
message: fillToolName(cancelRequested ? messages.cancelRequested : messages[status], statusToolName),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
if (status === "succeeded") {
|
|
45
|
+
const result = options.readResult === undefined ? undefined : await options.readResult({ id: job.id, queue: job.queue, payload: job.payload, finishedAt: job.finishedAt });
|
|
46
|
+
return { ...base, ...(result === undefined ? {} : { result }), message: messages.succeeded };
|
|
47
|
+
}
|
|
48
|
+
if (status === "cancelled")
|
|
49
|
+
return { ...base, errorCode: JOB_CANCELLED_CODE, message: messages.cancelled };
|
|
50
|
+
return { ...base, ...(job.lastError === null ? {} : { errorCode: job.lastError }), message: messages.failed };
|
|
51
|
+
}
|
|
52
|
+
/** The job behind the caller's operation, or null: not a job, not an operation, or another account's. */
|
|
53
|
+
export async function findOwnedJob(ctx, identity, operationId) {
|
|
54
|
+
const job = await getJob(ctx, operationId);
|
|
55
|
+
if (job === null || job.origin !== OPERATION_ORIGIN || job.dedupKey?.startsWith(getOwnerPrefix(identity.userId)) !== true)
|
|
56
|
+
return null;
|
|
57
|
+
return job;
|
|
58
|
+
}
|
|
59
|
+
export function getOwnerPrefix(userId) {
|
|
60
|
+
return `${OPERATION_ORIGIN}:${userId}:`;
|
|
61
|
+
}
|
|
62
|
+
export function getPollMin(poll = {}) {
|
|
63
|
+
return poll.minSeconds ?? DEFAULT_MIN_POLL_SECONDS;
|
|
64
|
+
}
|
|
65
|
+
export function fillToolName(text, toolName) {
|
|
66
|
+
return text.replaceAll("{tool}", toolName);
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=operation-state.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"operation-state.js","sourceRoot":"","sources":["../../src/jobs/operation-state.ts"],"names":[],"mappings":"AAAA,kHAAkH;AAClH,6FAA6F;AAC7F,OAAO,EAAE,kBAAkB,EAA8B,MAAM,kBAAkB,CAAC;AAClF,OAAO,EAAE,MAAM,EAAoB,MAAM,yBAAyB,CAAC;AAEnE,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAE5D,oGAAoG;AACpG,MAAM,CAAC,MAAM,gBAAgB,GAAG,KAAK,CAAC;AACtC,MAAM,CAAC,MAAM,0BA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AgB;IACzD,OAAO,IAAI,CAAC,UAAU,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;AAC7C,CAAC"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { type CallToolResult, type McpServer } from "@modelcontextprotocol/server";
|
|
2
|
+
import { type JobsContext } from "@softure-ai/jobs/server";
|
|
3
|
+
import type { McpServerIdentity } from "../contract.js";
|
|
4
|
+
import { type OperationPollOptions, type ReadOperationResult } from "./operation-state.js";
|
|
5
|
+
/** The extension's identifier, in the client's per-request capabilities and the server's `capabilities.extensions`. */
|
|
6
|
+
export declare const TASKS_EXTENSION = "io.modelcontextprotocol/tasks";
|
|
7
|
+
export declare const DEFAULT_TASK_TTL_SECONDS = 86400;
|
|
8
|
+
export declare const MIN_TASK_TTL_SECONDS = 60;
|
|
9
|
+
/** Thirty days: a task outlives its operation only so long. */
|
|
10
|
+
export declare const MAX_TASK_TTL_SECONDS = 2592000;
|
|
11
|
+
/** The tool handler's request context (its second argument): only its per-request `_meta` envelope is read. */
|
|
12
|
+
export interface OperationTaskRequest {
|
|
13
|
+
readonly mcpReq: {
|
|
14
|
+
readonly envelope?: object;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/** A task's status as the extension defines it; operations never ask for input, nor fail at the protocol level. */
|
|
18
|
+
export type OperationTaskStatus = "working" | "completed" | "cancelled";
|
|
19
|
+
/** A task as `tasks/get` answers it. `result` once completed: the `get_operation` answer, `isError` when it failed. */
|
|
20
|
+
export interface OperationTask {
|
|
21
|
+
readonly taskId: string;
|
|
22
|
+
readonly status: OperationTaskStatus;
|
|
23
|
+
readonly statusMessage?: string;
|
|
24
|
+
/** ISO 8601. */
|
|
25
|
+
readonly createdAt: string;
|
|
26
|
+
/** ISO 8601: the job's last change. */
|
|
27
|
+
readonly lastUpdatedAt: string;
|
|
28
|
+
readonly ttlMs: number;
|
|
29
|
+
/** Only while working: the operation's wait (`pollAfterSeconds`) in milliseconds. */
|
|
30
|
+
readonly pollIntervalMs?: number;
|
|
31
|
+
readonly result?: CallToolResult;
|
|
32
|
+
}
|
|
33
|
+
/** `ctx`, or `getContext` resolved inside each task request (see `OperationToolContext`). */
|
|
34
|
+
export type OperationTasksOptions = OperationTaskContext & {
|
|
35
|
+
/** The request's identity: a task is found only for its owner, and only a writer may cancel it. */
|
|
36
|
+
readonly identity: McpServerIdentity;
|
|
37
|
+
readonly readResult?: ReadOperationResult;
|
|
38
|
+
readonly poll?: OperationPollOptions;
|
|
39
|
+
};
|
|
40
|
+
type OperationTaskContext = {
|
|
41
|
+
readonly ctx: JobsContext;
|
|
42
|
+
readonly getContext?: never;
|
|
43
|
+
} | {
|
|
44
|
+
readonly ctx?: never;
|
|
45
|
+
readonly getContext: () => Promise<JobsContext>;
|
|
46
|
+
};
|
|
47
|
+
export interface AnswerOperationTaskInput {
|
|
48
|
+
readonly jobId: string;
|
|
49
|
+
readonly ttlSeconds?: number;
|
|
50
|
+
readonly poll?: OperationPollOptions;
|
|
51
|
+
}
|
|
52
|
+
/** True when the request's client declared the Tasks extension in its per-request capabilities (revision 2026-07-28). */
|
|
53
|
+
export declare function isTasksRequest(request: OperationTaskRequest): boolean;
|
|
54
|
+
export declare function isTaskTtlValid(seconds: number): boolean;
|
|
55
|
+
/**
|
|
56
|
+
* The tool's answer as a task (`resultType: "task"`) for the operation's job: stores the task before answering, as the
|
|
57
|
+
* spec requires, and gives the same account the same task id for the same job.
|
|
58
|
+
*/
|
|
59
|
+
export declare function answerOperationTask(ctx: JobsContext, identity: McpServerIdentity, input: AnswerOperationTaskInput): Promise<CallToolResult>;
|
|
60
|
+
/** The caller's task with its operation's state now; `null` when unknown, another account's, or its job is gone. */
|
|
61
|
+
export declare function getOperationTask(ctx: JobsContext, identity: McpServerIdentity, taskId: string, options?: Pick<OperationTasksOptions, "readResult" | "poll">): Promise<OperationTask | "expired" | null>;
|
|
62
|
+
/**
|
|
63
|
+
* Serves the Tasks extension on the app's server: declares the capability and answers `tasks/get`, `tasks/update` and
|
|
64
|
+
* `tasks/cancel` for the request's account. Register it on every server whose tools pass `taskRequest`.
|
|
65
|
+
*/
|
|
66
|
+
export declare function registerOperationTasks(server: McpServer, options: OperationTasksOptions): void;
|
|
67
|
+
export {};
|
|
68
|
+
//# sourceMappingURL=operation-tasks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"operation-tasks.d.ts","sourceRoot":"","sources":["../../src/jobs/operation-tasks.ts"],"names":[],"mappings":"AAWA,OAAO,EAKL,KAAK,cAAc,EACnB,KAAK,SAAS,EACf,MAAM,8BAA8B,CAAC;AAEtC,OAAO,EAAoB,KAAK,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAG7E,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAIxD,OAAO,EAKL,KAAK,oBAAoB,EACzB,KAAK,mBAAmB,EACzB,MAAM,sBAAsB,CAAC;AAE9B,uHAAuH;AACvH,eAAO,MAAM,eAAe,kCAAkC,CAAC;AAC/D,eAAO,MAAM,wBAAwB,QAAS,CAAC;AAC/C,eAAO,MAAM,oBAAoB,KAAK,CAAC;AACvC,+DAA+D;AAC/D,eAAO,MAAM,oBAAoB,UAAY,CAAC;AAQ9C,+GAA+G;AAC/G,MAAM,WAAW,oBAAoB;IACnC,QAAQ,CAAC,MAAM,EAAE;QAAE,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CACjD;AAED,mHAAmH;AACnH,MAAM,MAAM,mBAAmB,GAAG,SAAS,GAAG,WAAW,GAAG,WAAW,CAAC;AAExE,uHAAuH;AACvH,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,MAAM,EAAE,mBAAmB,CAAC;IACrC,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC;IAChC,gBAAgB;IAChB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,uCA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G,EAAE,WAAW,EAChB,QAAQ,EAAE,iBAAiB,EAC3B,MAAM,EAAE,MAAM,EACd,OAAO,GAAE,IAAI,CAAC,qBAAqB,EAAE,YAAY,GAAG,MAAM,CAAM,GAC/D,OAAO,CAAC,aAAa,GAAG,SAAS,GAAG,IAAI,CAAC,CAI3C;AAED;;;GAGG;AACH,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,qBAAqB,GAAG,IAAI,CA6B9F"}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
// Long operations as MCP Tasks (#977): the Tasks extension `io.modelcontextprotocol/tasks` of protocol revision
|
|
2
|
+
// 2026-07-28. A client that declares the extension on a tool call gets a task instead of an operation id and follows it
|
|
3
|
+
// through `tasks/get`; every other client keeps the polling tools (`get_operation`, `cancel_operation`) of #952.
|
|
4
|
+
//
|
|
5
|
+
// registerOperationTasks(server, { ctx, identity, readResult: (job) => findRenderUrl(job.id) });
|
|
6
|
+
// server.registerTool("render_video", { inputSchema }, (args, request) =>
|
|
7
|
+
// startOperation(ctx, identity, { queue: "video.render", payload: args, taskRequest: request }));
|
|
8
|
+
//
|
|
9
|
+
// A task id is random (the spec requires ids nobody can guess) and lives in `mcp.operation_tasks` with its owner, the
|
|
10
|
+
// operation's job and an expiry. Every task request checks the owner too, and reads the job's state as it is now.
|
|
11
|
+
import { randomBytes } from "node:crypto";
|
|
12
|
+
import { CLIENT_CAPABILITIES_META_KEY, MissingRequiredClientCapabilityError, ProtocolError, ProtocolErrorCode, } from "@modelcontextprotocol/server";
|
|
13
|
+
import { requestJobCancel } from "@softure-ai/jobs/server";
|
|
14
|
+
import { and, eq, sql } from "drizzle-orm";
|
|
15
|
+
import { z } from "zod";
|
|
16
|
+
import { operationTasks } from "../schema.js";
|
|
17
|
+
import { getMcpAccessMessages } from "../server/options.js";
|
|
18
|
+
import { toolResult } from "../server/tools.js";
|
|
19
|
+
import { describeOperation, findOwnedJob, getOperationStatus, getPollAfterSeconds, } from "./operation-state.js";
|
|
20
|
+
/** The extension's identifier, in the client's per-request capabilities and the server's `capabilities.extensions`. */
|
|
21
|
+
export const TASKS_EXTENSION = "io.modelcontextprotocol/tasks";
|
|
22
|
+
export const DEFAULT_TASK_TTL_SECONDS = 86_400;
|
|
23
|
+
export const MIN_TASK_TTL_SECONDS = 60;
|
|
24
|
+
/** Thirty days: a task outlives its operation only so long. */
|
|
25
|
+
export const MAX_TASK_TTL_SECONDS = 2_592_000;
|
|
26
|
+
const TASK_ID_BYTES = 32;
|
|
27
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
28
|
+
const TASK_NOT_FOUND = "Failed to retrieve task: Task not found";
|
|
29
|
+
const TASK_EXPIRED = "Failed to retrieve task: Task has expired";
|
|
30
|
+
const CANCEL_NEEDS_WRITE = "Cancelling a task needs a token with write access";
|
|
31
|
+
const taskIdParams = z.looseObject({ taskId: z.string() });
|
|
32
|
+
const taskResult = z.looseObject({});
|
|
33
|
+
const clientCapabilities = z.looseObject({ extensions: z.record(z.string(), z.unknown()).optional() });
|
|
34
|
+
/** True when the request's client declared the Tasks extension in its per-request capabilities (revision 2026-07-28). */
|
|
35
|
+
export function isTasksRequest(request) {
|
|
36
|
+
const envelope = { ...request.mcpReq.envelope };
|
|
37
|
+
const capabilities = clientCapabilities.safeParse(envelope[CLIENT_CAPABILITIES_META_KEY]);
|
|
38
|
+
return capabilities.success && capabilities.data.extensions?.[TASKS_EXTENSION] !== undefined;
|
|
39
|
+
}
|
|
40
|
+
export function isTaskTtlValid(seconds) {
|
|
41
|
+
return Number.isInteger(seconds) && seconds >= MIN_TASK_TTL_SECONDS && seconds <= MAX_TASK_TTL_SECONDS;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The tool's answer as a task (`resultType: "task"`) for the operation's job: stores the task before answering, as the
|
|
45
|
+
* spec requires, and gives the same account the same task id for the same job.
|
|
46
|
+
*/
|
|
47
|
+
export async function answerOperationTask(ctx, identity, input) {
|
|
48
|
+
const row = await storeTask(ctx, identity, input);
|
|
49
|
+
const job = await findOwnedJob(ctx, identity, input.jobId);
|
|
50
|
+
if (job === null)
|
|
51
|
+
throw new Error(`answerOperationTask: job ${input.jobId} is not an operation of this account`);
|
|
52
|
+
const task = await describeTask(ctx, row, job, input.poll === undefined ? {} : { poll: input.poll });
|
|
53
|
+
// On revision 2026-07-28 the SDK passes a tool's own `resultType` through; its `CallToolResult` type does not know
|
|
54
|
+
// the extension's task shape, and the `content` it would add anyway is ignored by clients.
|
|
55
|
+
return { content: [], ...task, resultType: "task" };
|
|
56
|
+
}
|
|
57
|
+
/** The caller's task with its operation's state now; `null` when unknown, another account's, or its job is gone. */
|
|
58
|
+
export async function getOperationTask(ctx, identity, taskId, options = {}) {
|
|
59
|
+
const found = await findTask(ctx, identity, taskId);
|
|
60
|
+
if (found === null || found === "expired")
|
|
61
|
+
return found;
|
|
62
|
+
return describeTask(ctx, found.row, found.job, options);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Serves the Tasks extension on the app's server: declares the capability and answers `tasks/get`, `tasks/update` and
|
|
66
|
+
* `tasks/cancel` for the request's account. Register it on every server whose tools pass `taskRequest`.
|
|
67
|
+
*/
|
|
68
|
+
export function registerOperationTasks(server, options) {
|
|
69
|
+
const { identity } = options;
|
|
70
|
+
const resolveContext = () => (options.getContext === undefined ? Promise.resolve(options.ctx) : options.getContext());
|
|
71
|
+
const describeOptions = { ...(options.readResult === undefined ? {} : { readResult: options.readResult }), ...(options.poll === undefined ? {} : { poll: options.poll }) };
|
|
72
|
+
server.server.registerCapabilities({ extensions: { [TASKS_EXTENSION]: {} } });
|
|
73
|
+
server.server.setRequestHandler("tasks/get", { params: taskIdParams, result: taskResult }, async ({ taskId }, request) => {
|
|
74
|
+
requireTasksCapability(request);
|
|
75
|
+
return { ...(await requireTask(await resolveContext(), identity, taskId, describeOptions)) };
|
|
76
|
+
});
|
|
77
|
+
// Operations never ask for input, so there is nothing to update; an owned task is acknowledged, as the spec asks.
|
|
78
|
+
server.server.setRequestHandler("tasks/update", { params: taskIdParams, result: taskResult }, async ({ taskId }, request) => {
|
|
79
|
+
requireTasksCapability(request);
|
|
80
|
+
await requireTask(await resolveContext(), identity, taskId, describeOptions);
|
|
81
|
+
return {};
|
|
82
|
+
});
|
|
83
|
+
server.server.setRequestHandler("tasks/cancel", { params: taskIdParams, result: taskResult }, async ({ taskId }, request) => {
|
|
84
|
+
requireTasksCapability(request);
|
|
85
|
+
const ctx = await resolveContext();
|
|
86
|
+
const found = await findTask(ctx, identity, taskId);
|
|
87
|
+
if (found === null)
|
|
88
|
+
throw new ProtocolError(ProtocolErrorCode.InvalidParams, TASK_NOT_FOUND);
|
|
89
|
+
if (found === "expired")
|
|
90
|
+
throw new ProtocolError(ProtocolErrorCode.InvalidParams, TASK_EXPIRED);
|
|
91
|
+
if (!identity.canWrite)
|
|
92
|
+
throw new ProtocolError(ProtocolErrorCode.InvalidRequest, CANCEL_NEEDS_WRITE);
|
|
93
|
+
// Cancelling is cooperative: an operation that has already ended is acknowledged all the same.
|
|
94
|
+
await requestJobCancel(ctx, found.job.id);
|
|
95
|
+
return {};
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
async function storeTask(ctx, identity, input) {
|
|
99
|
+
const now = ctx.clock.now();
|
|
100
|
+
const expiresAt = new Date(now.getTime() + (input.ttlSeconds ?? DEFAULT_TASK_TTL_SECONDS) * 1000);
|
|
101
|
+
// One task per account and job: joining a running operation answers its task again, kept alive at least as long.
|
|
102
|
+
const [row] = await ctx.db
|
|
103
|
+
.insert(operationTasks)
|
|
104
|
+
.values({ taskId: randomBytes(TASK_ID_BYTES).toString("base64url"), userId: identity.userId, jobId: BigInt(input.jobId), createdAt: now, expiresAt })
|
|
105
|
+
.onConflictDoUpdate({
|
|
106
|
+
target: [operationTasks.userId, operationTasks.jobId],
|
|
107
|
+
set: { expiresAt: sql `greatest(${operationTasks.expiresAt}, excluded.expires_at)` },
|
|
108
|
+
})
|
|
109
|
+
.returning();
|
|
110
|
+
if (row === undefined)
|
|
111
|
+
throw new Error(`answerOperationTask: no task row for job ${input.jobId}`);
|
|
112
|
+
return row;
|
|
113
|
+
}
|
|
114
|
+
async function findTask(ctx, identity, taskId) {
|
|
115
|
+
if (!UUID.test(identity.userId))
|
|
116
|
+
return null;
|
|
117
|
+
const [row] = await ctx.db
|
|
118
|
+
.select()
|
|
119
|
+
.from(operationTasks)
|
|
120
|
+
.where(and(eq(operationTasks.taskId, taskId), eq(operationTasks.userId, identity.userId)))
|
|
121
|
+
.limit(1);
|
|
122
|
+
if (row === undefined)
|
|
123
|
+
return null;
|
|
124
|
+
if (row.expiresAt.getTime() <= ctx.clock.now().getTime())
|
|
125
|
+
return "expired";
|
|
126
|
+
const job = await findOwnedJob(ctx, identity, row.jobId.toString());
|
|
127
|
+
return job === null ? null : { row, job };
|
|
128
|
+
}
|
|
129
|
+
async function requireTask(ctx, identity, taskId, options) {
|
|
130
|
+
const task = await getOperationTask(ctx, identity, taskId, options);
|
|
131
|
+
if (task === null)
|
|
132
|
+
throw new ProtocolError(ProtocolErrorCode.InvalidParams, TASK_NOT_FOUND);
|
|
133
|
+
if (task === "expired")
|
|
134
|
+
throw new ProtocolError(ProtocolErrorCode.InvalidParams, TASK_EXPIRED);
|
|
135
|
+
return task;
|
|
136
|
+
}
|
|
137
|
+
/** The spec's rule for `tasks/*`: a client that did not declare the extension on this request gets `-32021`. */
|
|
138
|
+
function requireTasksCapability(request) {
|
|
139
|
+
if (!isTasksRequest(request))
|
|
140
|
+
throw new MissingRequiredClientCapabilityError({ requiredCapabilities: { extensions: { [TASKS_EXTENSION]: {} } } });
|
|
141
|
+
}
|
|
142
|
+
async function describeTask(ctx, row, job, options) {
|
|
143
|
+
const messages = getMcpAccessMessages(ctx.config).operations;
|
|
144
|
+
const status = getOperationStatus(job);
|
|
145
|
+
const base = {
|
|
146
|
+
taskId: row.taskId,
|
|
147
|
+
createdAt: row.createdAt.toISOString(),
|
|
148
|
+
lastUpdatedAt: job.updatedAt.toISOString(),
|
|
149
|
+
ttlMs: row.expiresAt.getTime() - row.createdAt.getTime(),
|
|
150
|
+
};
|
|
151
|
+
if (status === "queued" || status === "running") {
|
|
152
|
+
const ageSeconds = (ctx.clock.now().getTime() - job.createdAt.getTime()) / 1000;
|
|
153
|
+
const statusMessage = job.cancelRequestedAt !== null ? messages.taskCancelRequested : status === "queued" ? messages.taskQueued : messages.taskRunning;
|
|
154
|
+
return { ...base, status: "working", statusMessage, pollIntervalMs: getPollAfterSeconds(ageSeconds, options.poll) * 1000 };
|
|
155
|
+
}
|
|
156
|
+
if (status === "cancelled")
|
|
157
|
+
return { ...base, status: "cancelled", statusMessage: messages.cancelled };
|
|
158
|
+
// A failed job is a tool-level failure, not a JSON-RPC error: the spec makes it `completed` with `isError`.
|
|
159
|
+
const answer = await describeOperation(ctx, job, options);
|
|
160
|
+
const result = status === "failed" ? { ...toolResult(answer), isError: true } : toolResult(answer);
|
|
161
|
+
return { ...base, status: "completed", statusMessage: answer.message, result };
|
|
162
|
+
}
|
|
163
|
+
//# sourceMappingURL=operation-tasks.js.map
|