@blen/uspto-mcp-server 0.0.0-stage → 0.1.1

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.
Files changed (201) hide show
  1. package/CHANGELOG.md +52 -0
  2. package/LICENSE +202 -0
  3. package/NOTICE +55 -0
  4. package/README.md +229 -2
  5. package/SECURITY.md +142 -0
  6. package/dist/accounts/app.d.ts +18 -0
  7. package/dist/accounts/app.d.ts.map +1 -0
  8. package/dist/accounts/app.js +407 -0
  9. package/dist/accounts/app.js.map +1 -0
  10. package/dist/accounts/config.d.ts +21 -0
  11. package/dist/accounts/config.d.ts.map +1 -0
  12. package/dist/accounts/config.js +55 -0
  13. package/dist/accounts/config.js.map +1 -0
  14. package/dist/accounts/crypto.d.ts +18 -0
  15. package/dist/accounts/crypto.d.ts.map +1 -0
  16. package/dist/accounts/crypto.js +43 -0
  17. package/dist/accounts/crypto.js.map +1 -0
  18. package/dist/accounts/keyGates.d.ts +19 -0
  19. package/dist/accounts/keyGates.d.ts.map +1 -0
  20. package/dist/accounts/keyGates.js +47 -0
  21. package/dist/accounts/keyGates.js.map +1 -0
  22. package/dist/accounts/paths.d.ts +10 -0
  23. package/dist/accounts/paths.d.ts.map +1 -0
  24. package/dist/accounts/paths.js +10 -0
  25. package/dist/accounts/paths.js.map +1 -0
  26. package/dist/accounts/portal.d.ts +7 -0
  27. package/dist/accounts/portal.d.ts.map +1 -0
  28. package/dist/accounts/portal.js +225 -0
  29. package/dist/accounts/portal.js.map +1 -0
  30. package/dist/accounts/provider.d.ts +2610 -0
  31. package/dist/accounts/provider.d.ts.map +1 -0
  32. package/dist/accounts/provider.js +108 -0
  33. package/dist/accounts/provider.js.map +1 -0
  34. package/dist/accounts/runtime.d.ts +26 -0
  35. package/dist/accounts/runtime.d.ts.map +1 -0
  36. package/dist/accounts/runtime.js +168 -0
  37. package/dist/accounts/runtime.js.map +1 -0
  38. package/dist/accounts/store.d.ts +30 -0
  39. package/dist/accounts/store.d.ts.map +1 -0
  40. package/dist/accounts/store.js +155 -0
  41. package/dist/accounts/store.js.map +1 -0
  42. package/dist/bin.d.ts +3 -0
  43. package/dist/bin.d.ts.map +1 -0
  44. package/dist/bin.js +195 -0
  45. package/dist/bin.js.map +1 -0
  46. package/dist/index.d.ts +11 -0
  47. package/dist/index.d.ts.map +1 -0
  48. package/dist/index.js +8 -0
  49. package/dist/index.js.map +1 -0
  50. package/dist/sandbox/deno.d.ts +35 -0
  51. package/dist/sandbox/deno.d.ts.map +1 -0
  52. package/dist/sandbox/deno.js +373 -0
  53. package/dist/sandbox/deno.js.map +1 -0
  54. package/dist/sandbox/index.d.ts +28 -0
  55. package/dist/sandbox/index.d.ts.map +1 -0
  56. package/dist/sandbox/index.js +55 -0
  57. package/dist/sandbox/index.js.map +1 -0
  58. package/dist/sandbox/isolate.d.ts +17 -0
  59. package/dist/sandbox/isolate.d.ts.map +1 -0
  60. package/dist/sandbox/isolate.js +195 -0
  61. package/dist/sandbox/isolate.js.map +1 -0
  62. package/dist/sandbox/limits.d.ts +60 -0
  63. package/dist/sandbox/limits.d.ts.map +1 -0
  64. package/dist/sandbox/limits.js +86 -0
  65. package/dist/sandbox/limits.js.map +1 -0
  66. package/dist/sandbox/policy.d.ts +7 -0
  67. package/dist/sandbox/policy.d.ts.map +1 -0
  68. package/dist/sandbox/policy.js +87 -0
  69. package/dist/sandbox/policy.js.map +1 -0
  70. package/dist/sandbox/types.d.ts +57 -0
  71. package/dist/sandbox/types.d.ts.map +1 -0
  72. package/dist/sandbox/types.js +6 -0
  73. package/dist/sandbox/types.js.map +1 -0
  74. package/dist/sdk/bindings.d.ts +13 -0
  75. package/dist/sdk/bindings.d.ts.map +1 -0
  76. package/dist/sdk/bindings.js +22 -0
  77. package/dist/sdk/bindings.js.map +1 -0
  78. package/dist/sdk/odp-client.d.ts +167 -0
  79. package/dist/sdk/odp-client.d.ts.map +1 -0
  80. package/dist/sdk/odp-client.js +253 -0
  81. package/dist/sdk/odp-client.js.map +1 -0
  82. package/dist/sdk/runtime.d.ts +21 -0
  83. package/dist/sdk/runtime.d.ts.map +1 -0
  84. package/dist/sdk/runtime.js +32 -0
  85. package/dist/sdk/runtime.js.map +1 -0
  86. package/dist/sdk/sources/corpus-loader.d.ts +8 -0
  87. package/dist/sdk/sources/corpus-loader.d.ts.map +1 -0
  88. package/dist/sdk/sources/corpus-loader.js +17 -0
  89. package/dist/sdk/sources/corpus-loader.js.map +1 -0
  90. package/dist/sdk/sources/index.d.ts +8 -0
  91. package/dist/sdk/sources/index.d.ts.map +1 -0
  92. package/dist/sdk/sources/index.js +25 -0
  93. package/dist/sdk/sources/index.js.map +1 -0
  94. package/dist/sdk/sources/odp.d.ts +5 -0
  95. package/dist/sdk/sources/odp.d.ts.map +1 -0
  96. package/dist/sdk/sources/odp.js +78 -0
  97. package/dist/sdk/sources/odp.js.map +1 -0
  98. package/dist/sdk/sources/source.d.ts +44 -0
  99. package/dist/sdk/sources/source.d.ts.map +1 -0
  100. package/dist/sdk/sources/source.js +3 -0
  101. package/dist/sdk/sources/source.js.map +1 -0
  102. package/dist/sdk/sources/tsdr.d.ts +5 -0
  103. package/dist/sdk/sources/tsdr.d.ts.map +1 -0
  104. package/dist/sdk/sources/tsdr.js +38 -0
  105. package/dist/sdk/sources/tsdr.js.map +1 -0
  106. package/dist/sdk/tsdr-client.d.ts +24 -0
  107. package/dist/sdk/tsdr-client.d.ts.map +1 -0
  108. package/dist/sdk/tsdr-client.js +65 -0
  109. package/dist/sdk/tsdr-client.js.map +1 -0
  110. package/dist/search/bm25.d.ts +25 -0
  111. package/dist/search/bm25.d.ts.map +1 -0
  112. package/dist/search/bm25.js +67 -0
  113. package/dist/search/bm25.js.map +1 -0
  114. package/dist/search/corpus.d.ts +24 -0
  115. package/dist/search/corpus.d.ts.map +1 -0
  116. package/dist/search/corpus.js +23 -0
  117. package/dist/search/corpus.js.map +1 -0
  118. package/dist/server/authz.d.ts +40 -0
  119. package/dist/server/authz.d.ts.map +1 -0
  120. package/dist/server/authz.js +70 -0
  121. package/dist/server/authz.js.map +1 -0
  122. package/dist/server/http.d.ts +44 -0
  123. package/dist/server/http.d.ts.map +1 -0
  124. package/dist/server/http.js +199 -0
  125. package/dist/server/http.js.map +1 -0
  126. package/dist/server/ipRateLimiter.d.ts +22 -0
  127. package/dist/server/ipRateLimiter.d.ts.map +1 -0
  128. package/dist/server/ipRateLimiter.js +58 -0
  129. package/dist/server/ipRateLimiter.js.map +1 -0
  130. package/dist/server/mcpServer.d.ts +22 -0
  131. package/dist/server/mcpServer.d.ts.map +1 -0
  132. package/dist/server/mcpServer.js +90 -0
  133. package/dist/server/mcpServer.js.map +1 -0
  134. package/dist/server/stdio.d.ts +3 -0
  135. package/dist/server/stdio.d.ts.map +1 -0
  136. package/dist/server/stdio.js +16 -0
  137. package/dist/server/stdio.js.map +1 -0
  138. package/dist/server/toolCatalog.d.ts +162 -0
  139. package/dist/server/toolCatalog.d.ts.map +1 -0
  140. package/dist/server/toolCatalog.js +83 -0
  141. package/dist/server/toolCatalog.js.map +1 -0
  142. package/dist/server/wellKnown.d.ts +16 -0
  143. package/dist/server/wellKnown.d.ts.map +1 -0
  144. package/dist/server/wellKnown.js +17 -0
  145. package/dist/server/wellKnown.js.map +1 -0
  146. package/dist/server/zodToJsonSchema.d.ts +8 -0
  147. package/dist/server/zodToJsonSchema.d.ts.map +1 -0
  148. package/dist/server/zodToJsonSchema.js +14 -0
  149. package/dist/server/zodToJsonSchema.js.map +1 -0
  150. package/dist/supervisor/index.d.ts +54 -0
  151. package/dist/supervisor/index.d.ts.map +1 -0
  152. package/dist/supervisor/index.js +62 -0
  153. package/dist/supervisor/index.js.map +1 -0
  154. package/dist/tools/describeSchema.d.ts +23 -0
  155. package/dist/tools/describeSchema.d.ts.map +1 -0
  156. package/dist/tools/describeSchema.js +14 -0
  157. package/dist/tools/describeSchema.js.map +1 -0
  158. package/dist/tools/execute.d.ts +36 -0
  159. package/dist/tools/execute.d.ts.map +1 -0
  160. package/dist/tools/execute.js +70 -0
  161. package/dist/tools/execute.js.map +1 -0
  162. package/dist/tools/searchApi.d.ts +21 -0
  163. package/dist/tools/searchApi.d.ts.map +1 -0
  164. package/dist/tools/searchApi.js +14 -0
  165. package/dist/tools/searchApi.js.map +1 -0
  166. package/dist/util/executionContext.d.ts +6 -0
  167. package/dist/util/executionContext.d.ts.map +1 -0
  168. package/dist/util/executionContext.js +26 -0
  169. package/dist/util/executionContext.js.map +1 -0
  170. package/dist/util/executionLimiter.d.ts +19 -0
  171. package/dist/util/executionLimiter.d.ts.map +1 -0
  172. package/dist/util/executionLimiter.js +41 -0
  173. package/dist/util/executionLimiter.js.map +1 -0
  174. package/dist/util/httpClient.d.ts +116 -0
  175. package/dist/util/httpClient.d.ts.map +1 -0
  176. package/dist/util/httpClient.js +434 -0
  177. package/dist/util/httpClient.js.map +1 -0
  178. package/dist/util/logger.d.ts +7 -0
  179. package/dist/util/logger.d.ts.map +1 -0
  180. package/dist/util/logger.js +25 -0
  181. package/dist/util/logger.js.map +1 -0
  182. package/dist/util/quotas.d.ts +13 -0
  183. package/dist/util/quotas.d.ts.map +1 -0
  184. package/dist/util/quotas.js +29 -0
  185. package/dist/util/quotas.js.map +1 -0
  186. package/dist/util/tokenBucket.d.ts +11 -0
  187. package/dist/util/tokenBucket.d.ts.map +1 -0
  188. package/dist/util/tokenBucket.js +30 -0
  189. package/dist/util/tokenBucket.js.map +1 -0
  190. package/dist/util/upstreamGate.d.ts +36 -0
  191. package/dist/util/upstreamGate.d.ts.map +1 -0
  192. package/dist/util/upstreamGate.js +112 -0
  193. package/dist/util/upstreamGate.js.map +1 -0
  194. package/dist/web/THIRD_PARTY_NOTICES.txt +1940 -0
  195. package/dist/web/assets/blen-logo-BKJFojyj.png +0 -0
  196. package/dist/web/assets/index-Dbbu8lq3.js +317 -0
  197. package/dist/web/assets/index-DkKvyv-b.css +2 -0
  198. package/dist/web/index.html +14 -0
  199. package/package.json +127 -4
  200. package/schema/odp.json +743 -0
  201. package/schema/tsdr.json +129 -0
@@ -0,0 +1,162 @@
1
+ import type { ToolAnnotations } from '@modelcontextprotocol/server';
2
+ import { type RequestCtx } from '../tools/execute.js';
3
+ import type { Sdk } from '../sdk/bindings.js';
4
+ import type { SandboxRunner } from '../sandbox/types.js';
5
+ import type { Corpus } from '../search/corpus.js';
6
+ import type { SubjectQuota } from '../util/quotas.js';
7
+ import type { ExecutionLimiter } from '../util/executionLimiter.js';
8
+ export interface ToolDescriptor {
9
+ name: string;
10
+ title: string;
11
+ description: string;
12
+ annotations: ToolAnnotations;
13
+ inputSchema: Record<string, unknown>;
14
+ /** JSON Schema of the structured result (`structuredContent`). */
15
+ outputSchema: Record<string, unknown>;
16
+ /** `signal` aborts when the client cancels the request or disconnects. */
17
+ handler: (args: unknown, ctx?: {
18
+ signal?: AbortSignal;
19
+ }) => Promise<unknown>;
20
+ /** True when a returned result is a tool execution failure (MCP `isError`). */
21
+ isFailure?: (result: unknown) => boolean;
22
+ }
23
+ export declare const OUTPUT_SCHEMAS: {
24
+ readonly search_api: {
25
+ readonly type: 'object';
26
+ readonly properties: {
27
+ readonly hits: {
28
+ readonly type: 'array';
29
+ readonly items: {
30
+ readonly type: 'object';
31
+ readonly properties: {
32
+ readonly id: {
33
+ readonly type: 'string';
34
+ };
35
+ readonly kind: {
36
+ readonly type: 'string';
37
+ readonly enum: readonly ['endpoint', 'field'];
38
+ };
39
+ readonly binding: {
40
+ readonly type: 'string';
41
+ };
42
+ readonly description: {
43
+ readonly type: 'string';
44
+ };
45
+ readonly signature: {
46
+ readonly type: 'string';
47
+ };
48
+ readonly example: {
49
+ readonly type: 'string';
50
+ };
51
+ readonly score: {
52
+ readonly type: 'number';
53
+ };
54
+ };
55
+ readonly required: readonly ['id', 'kind', 'binding', 'description'];
56
+ };
57
+ };
58
+ readonly note: {
59
+ readonly type: 'string';
60
+ };
61
+ };
62
+ readonly required: readonly ['hits', 'note'];
63
+ };
64
+ readonly describe_schema: {
65
+ readonly type: 'object';
66
+ readonly properties: {
67
+ readonly found: {
68
+ readonly type: 'boolean';
69
+ };
70
+ readonly entries: {
71
+ readonly type: 'array';
72
+ readonly items: {
73
+ readonly type: 'object';
74
+ readonly properties: {
75
+ readonly id: {
76
+ readonly type: 'string';
77
+ };
78
+ readonly kind: {
79
+ readonly type: 'string';
80
+ readonly enum: readonly ['endpoint', 'field'];
81
+ };
82
+ readonly binding: {
83
+ readonly type: 'string';
84
+ };
85
+ readonly description: {
86
+ readonly type: 'string';
87
+ };
88
+ readonly signature: {
89
+ readonly type: 'string';
90
+ };
91
+ readonly example: {
92
+ readonly type: 'string';
93
+ };
94
+ readonly score: {
95
+ readonly type: 'number';
96
+ };
97
+ };
98
+ readonly required: readonly ['id', 'kind', 'binding', 'description'];
99
+ };
100
+ };
101
+ readonly message: {
102
+ readonly type: 'string';
103
+ };
104
+ };
105
+ readonly required: readonly ['found'];
106
+ };
107
+ readonly execute: {
108
+ readonly type: 'object';
109
+ readonly properties: {
110
+ readonly ok: {
111
+ readonly type: 'boolean';
112
+ readonly description: 'false when the code threw, timed out, or broke a limit; see error';
113
+ };
114
+ readonly value: {
115
+ readonly description: 'The JSON value the code returned';
116
+ };
117
+ readonly logs: {
118
+ readonly type: 'array';
119
+ readonly items: {
120
+ readonly type: 'string';
121
+ };
122
+ };
123
+ readonly error: {
124
+ readonly type: 'object';
125
+ readonly properties: {
126
+ readonly name: {
127
+ readonly type: 'string';
128
+ };
129
+ readonly message: {
130
+ readonly type: 'string';
131
+ };
132
+ readonly status: {
133
+ readonly type: 'number';
134
+ };
135
+ };
136
+ readonly required: readonly ['name', 'message'];
137
+ };
138
+ readonly durationMs: {
139
+ readonly type: 'number';
140
+ };
141
+ };
142
+ readonly required: readonly ['ok', 'logs', 'durationMs'];
143
+ };
144
+ };
145
+ export interface CatalogDeps {
146
+ sdk: Sdk;
147
+ sdkForSubject?: (subject?: string) => Promise<Sdk>;
148
+ sandbox: SandboxRunner;
149
+ corpus: Corpus;
150
+ /** Upstream calls one execute may make, per binding. */
151
+ callBudgets: Readonly<Record<string, number>>;
152
+ /** Per-subject hourly quotas by binding (HTTP only; replaced by PostgreSQL limits in account mode). */
153
+ subjectQuotas?: Readonly<Record<string, SubjectQuota>>;
154
+ /** Caps simultaneous executions (global and per subject). */
155
+ executionLimiter?: ExecutionLimiter;
156
+ /** Aborts all executions on shutdown. */
157
+ shutdownSignal?: AbortSignal;
158
+ /** Tools to register; default all three. Account mode's anonymous endpoint omits `execute`. */
159
+ tools?: ReadonlyArray<'search_api' | 'describe_schema' | 'execute'>;
160
+ }
161
+ export declare function buildCatalog(deps: CatalogDeps, requestCtx?: RequestCtx): ToolDescriptor[];
162
+ //# sourceMappingURL=toolCatalog.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toolCatalog.d.ts","sourceRoot":"","sources":["../../src/server/toolCatalog.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAGpE,OAAO,EAAyB,KAAK,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAE7E,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAClD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAEpE,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,eAAe,CAAC;IAC7B,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACrC,kEAAkE;IAClE,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC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ools/describeSchema.js';
3
+ import { ExecuteInput, execute } from '../tools/execute.js';
4
+ import { zodToJsonSchema } from './zodToJsonSchema.js';
5
+ const ENTRY_SCHEMA = {
6
+ type: 'object',
7
+ properties: {
8
+ id: { type: 'string' }, kind: { type: 'string', enum: ['endpoint', 'field'] }, binding: { type: 'string' },
9
+ description: { type: 'string' }, signature: { type: 'string' }, example: { type: 'string' }, score: { type: 'number' },
10
+ },
11
+ required: ['id', 'kind', 'binding', 'description'],
12
+ };
13
+ export const OUTPUT_SCHEMAS = {
14
+ search_api: {
15
+ type: 'object',
16
+ properties: { hits: { type: 'array', items: ENTRY_SCHEMA }, note: { type: 'string' } },
17
+ required: ['hits', 'note'],
18
+ },
19
+ describe_schema: {
20
+ type: 'object',
21
+ properties: { found: { type: 'boolean' }, entries: { type: 'array', items: ENTRY_SCHEMA }, message: { type: 'string' } },
22
+ required: ['found'],
23
+ },
24
+ execute: {
25
+ type: 'object',
26
+ properties: {
27
+ ok: { type: 'boolean', description: 'false when the code threw, timed out, or broke a limit; see error' },
28
+ value: { description: 'The JSON value the code returned' },
29
+ logs: { type: 'array', items: { type: 'string' } },
30
+ error: {
31
+ type: 'object',
32
+ properties: { name: { type: 'string' }, message: { type: 'string' }, status: { type: 'number' } },
33
+ required: ['name', 'message'],
34
+ },
35
+ durationMs: { type: 'number' },
36
+ },
37
+ required: ['ok', 'logs', 'durationMs'],
38
+ },
39
+ };
40
+ export function buildCatalog(deps, requestCtx) {
41
+ const all = allTools(deps, requestCtx);
42
+ return deps.tools ? all.filter(t => deps.tools.includes(t.name)) : all;
43
+ }
44
+ function allTools(deps, requestCtx) {
45
+ const names = deps.sdk.registeredNames.join(', ');
46
+ const namespaces = deps.sdk.registeredNames.map(n => `${n}.*`).join(', ');
47
+ return [
48
+ {
49
+ name: 'search_api',
50
+ title: 'Search USPTO API Documentation',
51
+ annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false },
52
+ description: `BM25 search over the SDK methods and response-field dictionary for all bound sources (${names}): patent applications (file wrapper), PTAB trials/appeals/interferences, petition decisions, office actions, bulk datasets, and trademark status. Returns method signatures and JavaScript examples for use in execute. Start here.`,
53
+ inputSchema: zodToJsonSchema(SearchApiInput),
54
+ outputSchema: OUTPUT_SCHEMAS.search_api,
55
+ handler: async (args) => searchApi(SearchApiInput.parse(args), deps.corpus),
56
+ },
57
+ {
58
+ name: 'describe_schema',
59
+ title: 'Describe USPTO API Schema',
60
+ annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false },
61
+ description: `Look up an endpoint or field by exact dotted id (path) or by namespace prefix. Use to drill into the bound source surfaces (${namespaces}).`,
62
+ inputSchema: zodToJsonSchema(DescribeSchemaInput, {
63
+ // Exactly one of path / prefix (the handler's refine rule, in JSON Schema form).
64
+ oneOf: [{ required: ['path'], not: { required: ['prefix'] } }, { required: ['prefix'], not: { required: ['path'] } }],
65
+ }),
66
+ outputSchema: OUTPUT_SCHEMAS.describe_schema,
67
+ handler: async (args) => describeSchema(DescribeSchemaInput.parse(args), deps.corpus.entries),
68
+ },
69
+ {
70
+ name: 'execute',
71
+ title: 'Query USPTO Patent and Trademark Data',
72
+ // The sandbox SDK only reads upstream data. Idempotence describes external
73
+ // effects, not identical results; annotations do not enforce permissions.
74
+ annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: true },
75
+ description: `Run JavaScript inside a sandbox (no net, fs, env, or subprocess). Plain JS only — TypeScript type annotations are rejected. Globals: ${names}. Top-level await is allowed; \`return\` the value you want as the result. USPTO serves one request at a time per API key, so calls are queued: prefer one well-filtered search (with \`fields\` to trim large records) over many lookups, and return only what you need.`,
76
+ inputSchema: zodToJsonSchema(ExecuteInput),
77
+ outputSchema: OUTPUT_SCHEMAS.execute,
78
+ handler: async (args, ctx) => execute(ExecuteInput.parse(args), deps, { ...requestCtx, ...(ctx?.signal ? { signal: ctx.signal } : {}) }),
79
+ isFailure: (result) => result?.ok === false,
80
+ },
81
+ ];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AG,EAAE,EAAE,QAAQ,EAAE,CAAC,MAAM,CAAC,EAAE,EAAE,CAAC;aACtH,CAAC;YACF,YAAY,EAAE,cAAc,CAAC,eAAe;YAC5C,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE,CAAC,cAAc,CAAC,mBAAmB,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC;SAC9F;QACD;YACE,IAAI,EAAE,SAAS;YACf,KAAK,EAAE,uCAAuC;YAC9C,2EAA2E;YAC3E,0EAA0E;YAC1E,WAAW,EAAE,EAAE,YAAY,EAAE,IAAI,EAAE,eAAe,EAAE,KAAK,EAAE,cAAc,EAAE,IAAI,EAAE,aAAa,EAAE,IAAI,EAAE;YACtG,WAAW,EACT,wIAAwI,KAAK,2QAA2Q;YAC1Z,WAAW,EAAE,eAAe,CAAC,YAAY,CAAC;YAC1C,YAAY,EAAE,cAAc,CAAC,OAAO;YACpC,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,EAAE,EAAE,CAAC,OAAO,CAAC,YAAY,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,EAAE,GAAG,UAAU,EAAE,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC;YACxI,SAAS,EAAE,CAAC,MAAM,EAAE,EAAE,CAAE,MAA2B,EAAE,EAAE,KAAK,KAAK;SAClE;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,16 @@
1
+ import type { AuthConfig } from './authz.js';
2
+ /**
3
+ * OAuth 2.0 Protected Resource Metadata document per RFC 9728.
4
+ *
5
+ * `resource` is REQUIRED and MUST be the canonical URL of the protected resource
6
+ * (the MCP endpoint, e.g. https://example.com/mcp).
7
+ */
8
+ export declare function oauthProtectedResource(cfg: AuthConfig, resourceUrl: string): {
9
+ resource: string;
10
+ authorization_servers: string[];
11
+ bearer_methods_supported: string[];
12
+ scopes_supported: string[];
13
+ resource_documentation: string;
14
+ resource_name: string;
15
+ };
16
+ //# sourceMappingURL=wellKnown.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wellKnown.d.ts","sourceRoot":"","sources":["../../src/server/wellKnown.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAE7C;;;;;GAKG;AACH,wBAAgB,sBAAsB,CAAC,GAAG,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM;IAEvE,QAAQ;IACR,qBAAqB;IACrB,wBAAwB;IACxB,gBAAgB;IAChB,sBAAsB;IACtB,aAAa;EAEhB"}
@@ -0,0 +1,17 @@
1
+ /**
2
+ * OAuth 2.0 Protected Resource Metadata document per RFC 9728.
3
+ *
4
+ * `resource` is REQUIRED and MUST be the canonical URL of the protected resource
5
+ * (the MCP endpoint, e.g. https://example.com/mcp).
6
+ */
7
+ export function oauthProtectedResource(cfg, resourceUrl) {
8
+ return {
9
+ resource: resourceUrl,
10
+ authorization_servers: cfg.issuer ? [cfg.issuer] : [],
11
+ bearer_methods_supported: ['header'],
12
+ scopes_supported: cfg.scopes ?? [],
13
+ resource_documentation: 'https://github.com/blen-labs/uspto-mcp-server#self-hosting-remote-http',
14
+ resource_name: 'Patents & Trademarks MCP',
15
+ };
16
+ }
17
+ //# sourceMappingURL=wellKnown.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wellKnown.js","sourceRoot":"","sources":["../../src/server/wellKnown.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,MAAM,UAAU,sBAAsB,CAAC,GAAe,EAAE,WAAmB;IACzE,OAAO;QACL,QAAQ,EAAE,WAAW;QACrB,qBAAqB,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE;QACrD,wBAAwB,EAAE,CAAC,QAAQ,CAAC;QACpC,gBAAgB,EAAE,GAAG,CAAC,MAAM,IAAI,EAAE;QAClC,sBAAsB,EAAE,wEAAwE;QAChG,aAAa,EAAE,0BAA0B;KAC1C,CAAC;AACJ,CAAC"}
@@ -0,0 +1,8 @@
1
+ import { z } from 'zod';
2
+ /**
3
+ * Tool input schema as advertised in tools/list, from Zod 4's official converter so the
4
+ * constraints the handler enforces (integer, min/max, length) are the ones clients see. Input
5
+ * mode: fields with defaults are optional. Unknown keys are rejected.
6
+ */
7
+ export declare function zodToJsonSchema(schema: z.ZodType, extra?: Record<string, unknown>): Record<string, unknown>;
8
+ //# sourceMappingURL=zodToJsonSchema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zodToJsonSchema.d.ts","sourceRoot":"","sources":["../../src/server/zodToJsonSchema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,CAAC,CAAC,OAAO,EAAE,KAAK,GAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAK/G"}
@@ -0,0 +1,14 @@
1
+ import { z } from 'zod';
2
+ /**
3
+ * Tool input schema as advertised in tools/list, from Zod 4's official converter so the
4
+ * constraints the handler enforces (integer, min/max, length) are the ones clients see. Input
5
+ * mode: fields with defaults are optional. Unknown keys are rejected.
6
+ */
7
+ export function zodToJsonSchema(schema, extra = {}) {
8
+ const out = z.toJSONSchema(schema, { io: 'input', unrepresentable: 'any' });
9
+ delete out.$schema;
10
+ if (out.type === 'object')
11
+ out.additionalProperties = false;
12
+ return { ...out, ...extra };
13
+ }
14
+ //# sourceMappingURL=zodToJsonSchema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zodToJsonSchema.js","sourceRoot":"","sources":["../../src/server/zodToJsonSchema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAAC,MAAiB,EAAE,KAAK,GAA4B,EAAE;IACpF,MAAM,GAAG,GAAG,CAAC,CAAC,YAAY,CAAC,MAAM,EAAE,EAAE,EAAE,EAAE,OAAO,EAAE,eAAe,EAAE,KAAK,EAAE,CAA4B,CAAC;IACvG,OAAO,GAAG,CAAC,OAAO,CAAC;IACnB,IAAI,GAAG,CAAC,IAAI,KAAK,QAAQ;QAAE,GAAG,CAAC,oBAAoB,GAAG,KAAK,CAAC;IAC5D,OAAO,EAAE,GAAG,GAAG,EAAE,GAAG,KAAK,EAAE,CAAC;AAC9B,CAAC"}
@@ -0,0 +1,54 @@
1
+ import type { SourceConfig, SourceName } from '../sdk/sources/source.js';
2
+ import { UpstreamGate } from '../util/upstreamGate.js';
3
+ import type { CatalogDeps } from '../server/toolCatalog.js';
4
+ import type { SandboxKind } from '../sandbox/types.js';
5
+ /** Guardrails for one API-key-gated upstream. */
6
+ export interface KeyedLimits {
7
+ /** Upstream calls one execute() may make. */
8
+ maxCallsPerExecute: number;
9
+ /** Process-wide hourly cap on the operator's shared key (after the cache). */
10
+ ratePerHour: number;
11
+ /** Per-subject hourly cap (HTTP transport only; stdio has no subject). */
12
+ subjectRatePerHour: number;
13
+ /** Requests in flight per API key. USPTO blocks concurrent requests with one key: keep 1. */
14
+ concurrency: number;
15
+ /** Requests started per second per API key. */
16
+ ratePerSec: number;
17
+ /** Longest a call waits for a slot before failing with RateLimited. */
18
+ maxWaitMs: number;
19
+ }
20
+ /**
21
+ * Defaults from https://data.uspto.gov/apis/api-rate-limits (burst 1 per key, 4-15 requests/s,
22
+ * 5M metadata + 1.2M document calls per week) and the TSDR key guide (60 requests/min).
23
+ */
24
+ export declare const LIMIT_DEFAULTS: Readonly<Record<SourceName, Readonly<KeyedLimits>>>;
25
+ export interface SupervisorConfig {
26
+ odpBaseUrl: string;
27
+ odpApiKey?: string;
28
+ tsdrBaseUrl: string;
29
+ tsdrApiKey?: string;
30
+ userAgent: string;
31
+ upstreamTimeoutMs: number;
32
+ upstreamRetries: number;
33
+ cacheTtlMs: number;
34
+ cacheMaxItems: number;
35
+ maxResponseBytes?: number;
36
+ sandbox: SandboxKind;
37
+ /** Prefer a process-isolated sandbox for `auto` (multi-user HTTP serving). */
38
+ processIsolation?: boolean;
39
+ limits: Record<SourceName, KeyedLimits>;
40
+ /** Simultaneous executions, server-wide and per subject. */
41
+ maxConcurrentExecutions?: number;
42
+ maxExecutionsPerSubject?: number;
43
+ /** Aborts every execution (graceful shutdown). */
44
+ shutdownSignal?: AbortSignal;
45
+ }
46
+ export declare const EXECUTION_DEFAULTS: {
47
+ readonly maxConcurrent: 8;
48
+ readonly perSubject: 2;
49
+ };
50
+ export declare function keyGate(l: KeyedLimits): UpstreamGate;
51
+ /** The parts of SourceConfig that do not depend on which key is used. */
52
+ export declare function baseSourceConfig(cfg: SupervisorConfig): Omit<SourceConfig, 'odp' | 'tsdr'>;
53
+ export declare function buildSupervisor(cfg: SupervisorConfig): Promise<CatalogDeps>;
54
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/supervisor/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAIzE,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAEvD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAGvD,iDAAiD;AACjD,MAAM,WAAW,WAAW;IAC1B,6CAA6C;IAC7C,kBAAkB,EAAE,MAAM,CAAC;IAC3B,8EAA8E;IAC9E,WAAW,EAAE,MAAM,CAAC;IACpB,0EAA0E;IAC1E,kBAAkB,EAAE,MAAM,CAA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BAAgB,GAAG,OAAO,CAAC,WAAW,CAAC,CA+BjF"}
@@ -0,0 +1,62 @@
1
+ import { sdkFromSources } from '../sdk/bindings.js';
2
+ import { getSources } from '../sdk/sources/index.js';
3
+ import { buildCorpus } from '../search/corpus.js';
4
+ import { pickSandbox } from '../sandbox/index.js';
5
+ import { TokenBucket } from '../util/tokenBucket.js';
6
+ import { UpstreamGate } from '../util/upstreamGate.js';
7
+ import { SubjectQuota } from '../util/quotas.js';
8
+ import { ExecutionLimiter } from '../util/executionLimiter.js';
9
+ /**
10
+ * Defaults from https://data.uspto.gov/apis/api-rate-limits (burst 1 per key, 4-15 requests/s,
11
+ * 5M metadata + 1.2M document calls per week) and the TSDR key guide (60 requests/min).
12
+ */
13
+ export const LIMIT_DEFAULTS = Object.freeze({
14
+ odp: Object.freeze({ maxCallsPerExecute: 50, ratePerHour: 5000, subjectRatePerHour: 1000, concurrency: 1, ratePerSec: 4, maxWaitMs: 30_000 }),
15
+ tsdr: Object.freeze({ maxCallsPerExecute: 25, ratePerHour: 3000, subjectRatePerHour: 600, concurrency: 1, ratePerSec: 1, maxWaitMs: 30_000 }),
16
+ });
17
+ export const EXECUTION_DEFAULTS = { maxConcurrent: 8, perSubject: 2 };
18
+ export function keyGate(l) {
19
+ return new UpstreamGate({
20
+ concurrency: l.concurrency, ratePerSec: l.ratePerSec,
21
+ // Never let a burst defeat the per-second pacing USPTO asks for.
22
+ burst: 1, maxWaitMs: l.maxWaitMs,
23
+ });
24
+ }
25
+ /** The parts of SourceConfig that do not depend on which key is used. */
26
+ export function baseSourceConfig(cfg) {
27
+ return {
28
+ userAgent: cfg.userAgent, timeoutMs: cfg.upstreamTimeoutMs, retries: cfg.upstreamRetries,
29
+ cacheTtlMs: cfg.cacheTtlMs, cacheMaxItems: cfg.cacheMaxItems,
30
+ ...(cfg.maxResponseBytes ? { maxResponseBytes: cfg.maxResponseBytes } : {}),
31
+ };
32
+ }
33
+ export async function buildSupervisor(cfg) {
34
+ const { odp, tsdr } = cfg.limits;
35
+ // One limiter and one gate per shared key.
36
+ const sourceCfg = {
37
+ ...baseSourceConfig(cfg),
38
+ odp: {
39
+ baseUrl: cfg.odpBaseUrl, apiKey: cfg.odpApiKey,
40
+ preflightLimiter: new TokenBucket(odp.ratePerHour / 3600, odp.ratePerHour), gate: keyGate(odp),
41
+ },
42
+ tsdr: {
43
+ baseUrl: cfg.tsdrBaseUrl, apiKey: cfg.tsdrApiKey,
44
+ preflightLimiter: new TokenBucket(tsdr.ratePerHour / 3600, tsdr.ratePerHour), gate: keyGate(tsdr),
45
+ },
46
+ };
47
+ const sources = getSources(sourceCfg);
48
+ const sdk = sdkFromSources(sources);
49
+ const corpus = buildCorpus(sources);
50
+ const sandbox = await pickSandbox(cfg.sandbox, { processIsolation: cfg.processIsolation });
51
+ return {
52
+ sdk, sandbox, corpus,
53
+ executionLimiter: new ExecutionLimiter(cfg.maxConcurrentExecutions ?? EXECUTION_DEFAULTS.maxConcurrent, cfg.maxExecutionsPerSubject ?? EXECUTION_DEFAULTS.perSubject),
54
+ ...(cfg.shutdownSignal ? { shutdownSignal: cfg.shutdownSignal } : {}),
55
+ callBudgets: { odp: odp.maxCallsPerExecute, tsdr: tsdr.maxCallsPerExecute },
56
+ subjectQuotas: {
57
+ odp: new SubjectQuota(odp.subjectRatePerHour, 60 * 60 * 1000),
58
+ tsdr: new SubjectQuota(tsdr.subjectRatePerHour, 60 * 60 * 1000),
59
+ },
60
+ };
61
+ }
62
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/supervisor/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAErD,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAClD,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAClD,OAAO,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AAM;QACpB,gBAAgB,EAAE,IAAI,gBAAgB,CACpC,GAAG,CAAC,uBAAuB,IAAI,kBAAkB,CAAC,aAAa,EAC/D,GAAG,CAAC,uBAAuB,IAAI,kBAAkB,CAAC,UAAU,CAC7D;QACD,GAAG,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC,CAAC,EAAE,cAAc,EAAE,GAAG,CAAC,cAAc,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACrE,WAAW,EAAE,EAAE,GAAG,EAAE,GAAG,CAAC,kBAAkB,EAAE,IAAI,EAAE,IAAI,CAAC,kBAAkB,EAAE;QAC3E,aAAa,EAAE;YACb,GAAG,EAAE,IAAI,YAAY,CAAC,GAAG,CAAC,kBAAkB,EAAE,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;YAC7D,IAAI,EAAE,IAAI,YAAY,CAAC,IAAI,CAAC,kBAAkB,EAAE,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;SAChE;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,23 @@
1
+ import { z } from 'zod';
2
+ import { type CorpusEntry } from '../search/corpus.js';
3
+ export declare const DescribeSchemaInput: z.ZodObject<{
4
+ path: z.ZodOptional<z.ZodString>;
5
+ prefix: z.ZodOptional<z.ZodString>;
6
+ }, z.core.$strip>;
7
+ export type DescribeSchemaInputT = z.infer<typeof DescribeSchemaInput>;
8
+ export type DescribeSchemaResult = {
9
+ found: false;
10
+ message: string;
11
+ } | {
12
+ found: true;
13
+ entries: Array<{
14
+ id: string;
15
+ kind: 'endpoint' | 'field';
16
+ binding: string;
17
+ description: string;
18
+ signature?: string;
19
+ example?: string;
20
+ }>;
21
+ };
22
+ export declare function describeSchema(input: DescribeSchemaInputT, entries: Map<string, CorpusEntry>): DescribeSchemaResult;
23
+ //# sourceMappingURL=describeSchema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"describeSchema.d.ts","sourceRoot":"","sources":["../../src/tools/describeSchema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAwB,KAAK,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAE7E,eAAO,MAAM,mBAAmB;;;iBAG+E,CAAC;AAChH,MAAM,MAAM,oBAAoB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,mBAAmB,CAAC,CAAC;AAEvE,MAAM,MAAM,oBAAoB,GAC5B;IAAE,KAAK,EAAE,KAAK,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GACjC;IAAE,KAAK,EAAE,IAAI,CAAC;IAAC,OAAO,EAAE,KAAK,CAAC;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC;QAAC,OAAO,EAAE,MAAM,CAAC;QAAC,WAAW,EAAE,MAAM,CAAC;QAAC,SAAS,CAAC,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC,CAAA;CAAE,CAAC;AAE5J,wBAAgB,cAAc,CAAC,KAAK,EAAE,oBAAoB,EAAE,OAAO,EAAE,GAAG,CAAC,MAAM,EAAE,WAAW,CAAC,GAAG,oBAAoB,CAMnH"}
@@ -0,0 +1,14 @@
1
+ import { z } from 'zod';
2
+ import { lookupByPathOrPrefix } from '../search/corpus.js';
3
+ export const DescribeSchemaInput = z.object({
4
+ path: z.string().optional().describe("Exact dotted id, e.g. 'odp.patents.search' or 'tsdr.status'"),
5
+ prefix: z.string().optional().describe("Prefix to enumerate, e.g. 'odp.ptab' or 'tsdr.'"),
6
+ }).refine(v => Boolean(v.path) !== Boolean(v.prefix), { message: 'Provide exactly one of `path` or `prefix`' });
7
+ export function describeSchema(input, entries) {
8
+ const hits = lookupByPathOrPrefix(entries, input);
9
+ if (hits.length === 0) {
10
+ return { found: false, message: `No entries matched ${input.path ? `path '${input.path}'` : `prefix '${input.prefix}'`}.` };
11
+ }
12
+ return { found: true, entries: hits.map(e => ({ id: e.id, kind: e.kind, binding: e.binding, description: e.description, signature: e.signature, example: e.example })) };
13
+ }
14
+ //# sourceMappingURL=describeSchema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"describeSchema.js","sourceRoot":"","sources":["../../src/tools/describeSchema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,oBAAoB,EAAoB,MAAM,qBAAqB,CAAC;AAE7E,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,CAAC,MAAM,CAAC;IAC1C,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,6DAA6D,CAAC;IACnG,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,iDAAiD,CAAC;CAC1F,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,EAAE,EAAE,OAAO,EAAE,2CAA2C,EAAE,CAAC,CAAC;AAOhH,MAAM,UAAU,cAAc,CAAC,KAA2B,EAAE,OAAiC;IAC3F,MAAM,IAAI,GAAG,oBAAoB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;IAClD,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACtB,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,sBAAsB,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,KAAK,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,WAAW,KAAK,CAAC,MAAM,GAAG,GAAG,EAAE,CAAC;IAC9H,CAAC;IACD,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC,OAAO,EAAE,WAAW,EAAE,CAAC,CAAC,WAAW,EAAE,SAAS,EAAE,CAAC,CAAC,SAAS,EAAE,OAAO,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,EAAE,CAAC;AAC3K,CAAC"}
@@ -0,0 +1,36 @@
1
+ import { z } from 'zod';
2
+ import type { Sdk } from '../sdk/bindings.js';
3
+ import { type SandboxRunner } from '../sandbox/types.js';
4
+ import { SubjectQuota } from '../util/quotas.js';
5
+ import { ExecutionLimiter } from '../util/executionLimiter.js';
6
+ export declare const ExecuteInput: z.ZodObject<{
7
+ code: z.ZodString;
8
+ timeoutMs: z.ZodDefault<z.ZodNumber>;
9
+ memoryMb: z.ZodDefault<z.ZodNumber>;
10
+ }, z.core.$strip>;
11
+ export type ExecuteInputT = z.infer<typeof ExecuteInput>;
12
+ export interface ExecuteDeps {
13
+ sdk: Sdk;
14
+ sdkForSubject?: (subject?: string) => Promise<Sdk>;
15
+ sandbox: SandboxRunner;
16
+ /** Upstream calls one execute may make, per binding. A binding without an entry gets DEFAULT_CALL_BUDGET. */
17
+ callBudgets: Readonly<Record<string, number>>;
18
+ /** Per-subject hourly quotas by binding. Absent in account mode, which limits per account in PostgreSQL. */
19
+ subjectQuotas?: Readonly<Record<string, SubjectQuota>>;
20
+ /** Caps simultaneous executions; omitted means unlimited (tests). */
21
+ executionLimiter?: ExecutionLimiter;
22
+ /** Aborts every execution, e.g. on shutdown. */
23
+ shutdownSignal?: AbortSignal;
24
+ }
25
+ export declare const DEFAULT_CALL_BUDGET = 25;
26
+ /**
27
+ * Per-request context, re-derived from the caller's bearer token on every HTTP request.
28
+ * stdio has no subject.
29
+ */
30
+ export interface RequestCtx {
31
+ subject?: string;
32
+ /** The MCP request's abort signal: the client cancelled or disconnected. */
33
+ signal?: AbortSignal;
34
+ }
35
+ export declare function execute(input: ExecuteInputT, deps: ExecuteDeps, requestCtx?: RequestCtx): Promise<import("../index.js").ExecuteResult>;
36
+ //# sourceMappingURL=execute.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"execute.d.ts","sourceRoot":"","sources":["../../src/tools/execute.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,oBAAoB,CAAC;AAE9C,OAAO,EAAY,KAAK,aAAa,EAAqB,MAAM,qBAAqB,CAAC;AACtF,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAI/D,eAAO,MAAM,YAAY;;;;iBAMvB,CAAC;AAEH,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,YAAY,CAAC,CAAC;AAEzD,MAAM,WAAW,WAAW;IAC1B,GAAG,EAAE,GAAG,CAAC;IACT,aAAa,CAAC,EAAE,CAAC,OAAO,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC;IACnD,OAAO,EAAE,aAAa,CAAC;IACvB,6GAA6G;IAC7G,WAAW,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;IAC9C,4GAA4G;IAC5G,aAAa,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC;IACvD,qEAAqE;IACrE,gBAAgB,CAAC,EAAE,gBAAgB,CAAC;IACpC,gDAAgD;IAChD,cAAc,CAAC,EAAE,WAAW,CAAC;CAC9B;AAED,eAAO,MAAM,mBAAmB,KAAK,CAAC;AAEtC;;;GAGG;AACH,MAAM,WAAW,UAAU;IACzB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,4EAA4E;IAC5E,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAED,wBAAsB,OAAO,CAAC,KAAK,EAAE,aAAa,EAAE,IAAI,EAAE,WAAW,EAAE,UAAU,CAAC,EAAE,UAAU,gDA0B7F"}
@@ -0,0 +1,70 @@
1
+ import { z } from 'zod';
2
+ import { dispatch } from '../sdk/runtime.js';
3
+ import { TEARDOWN } from '../sandbox/types.js';
4
+ import { runWithSignal } from '../util/executionContext.js';
5
+ import { DEFAULT_LIMITS } from '../sandbox/limits.js';
6
+ export const ExecuteInput = z.object({
7
+ code: z.string().min(1).max(DEFAULT_LIMITS.maxCodeBytes).describe('JavaScript to run inside the sandbox (plain JS: TypeScript type annotations are rejected; top-level await/return are allowed). SDK globals: `odp` (USPTO Open Data Portal: patents, PTAB, petitions, office actions, bulk datasets) and `tsdr` (trademark status).'),
8
+ // USPTO serves one request at a time per API key, so multi-call scripts need more time than
9
+ // a parallel API would.
10
+ timeoutMs: z.number().int().min(100).max(120_000).default(30_000),
11
+ memoryMb: z.number().int().min(16).max(256).default(64),
12
+ });
13
+ export const DEFAULT_CALL_BUDGET = 25;
14
+ export async function execute(input, deps, requestCtx) {
15
+ const upstream = [deps.shutdownSignal, requestCtx?.signal].filter((s) => Boolean(s));
16
+ if (upstream.some(s => s.aborted)) {
17
+ return { ok: false, logs: [], error: { name: 'AbortError', message: deps.shutdownSignal?.aborted ? 'The server is shutting down.' : 'The request was cancelled.' }, durationMs: 0 };
18
+ }
19
+ const admitted = deps.executionLimiter?.tryAcquire(requestCtx?.subject);
20
+ if (admitted && 'error' in admitted) {
21
+ return { ok: false, logs: [], error: { name: 'Busy', message: admitted.error }, durationMs: 0 };
22
+ }
23
+ // Aborted when this execution ends for any reason, or when the client cancels / disconnects or
24
+ // the server shuts down, so the sandbox and queued and in-flight upstream calls stop.
25
+ const controller = new AbortController();
26
+ const onAbort = () => controller.abort();
27
+ for (const s of upstream)
28
+ s.addEventListener('abort', onAbort, { once: true });
29
+ let teardown;
30
+ try {
31
+ const result = await runExecution(input, deps, requestCtx, controller.signal);
32
+ teardown = result[TEARDOWN];
33
+ return result;
34
+ }
35
+ finally {
36
+ controller.abort();
37
+ for (const s of upstream)
38
+ s.removeEventListener('abort', onAbort);
39
+ // A sandbox process that has not exited yet keeps its execution slot until it does.
40
+ if (teardown)
41
+ void teardown.then(() => admitted?.release());
42
+ else
43
+ admitted?.release();
44
+ }
45
+ }
46
+ async function runExecution(input, deps, requestCtx, signal) {
47
+ const sdk = deps.sdkForSubject ? await deps.sdkForSubject(requestCtx?.subject) : deps.sdk;
48
+ const calls = new Map();
49
+ const labelOf = (binding) => sdk.meta.find(m => m.name === binding)?.label ?? binding;
50
+ return deps.sandbox.execute({ code: input.code, timeoutMs: input.timeoutMs, memoryMb: input.memoryMb, bindings: sdk.registeredNames, signal }, {
51
+ dispatch: (req, ctx) => {
52
+ const binding = typeof req?.binding === 'string' ? req.binding : '';
53
+ const budget = Object.hasOwn(deps.callBudgets, binding) ? deps.callBudgets[binding] : DEFAULT_CALL_BUDGET;
54
+ const used = calls.get(binding) ?? 0;
55
+ if (used >= budget) {
56
+ return Promise.resolve({ ok: false, error: { name: 'CallBudgetExceeded', message: `Exceeded the per-execute ${labelOf(binding)} call budget (${budget}). Narrow the search (filters, fields, larger page size) or split the work across separate execute calls.` } });
57
+ }
58
+ const quota = requestCtx?.subject && deps.subjectQuotas && Object.hasOwn(deps.subjectQuotas, binding) ? deps.subjectQuotas[binding] : undefined;
59
+ if (quota) {
60
+ const q = quota.consume(requestCtx.subject);
61
+ if (!q.allowed) {
62
+ return Promise.resolve({ ok: false, error: { name: 'SubjectQuotaExceeded', message: `Your hourly ${labelOf(binding)} quota on this server is used up. Try again after ${new Date(q.resetAt).toISOString()}.` } });
63
+ }
64
+ }
65
+ calls.set(binding, used + 1);
66
+ return runWithSignal(ctx?.signal ?? signal, () => dispatch(sdk, req));
67
+ },
68
+ });
69
+ }
70
+ //# sourceMappingURL=execute.js.map