xmcp 0.6.6 → 0.6.7

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 (69) hide show
  1. package/README.md +32 -0
  2. package/dist/cli.js +30 -32
  3. package/dist/compiler/config/index.d.ts +54 -54
  4. package/dist/compiler/config/schemas/transport/http.d.ts +82 -82
  5. package/dist/index.d.ts +4 -2
  6. package/dist/index.js +3 -3
  7. package/dist/runtime/adapter-express.js +39 -22
  8. package/dist/runtime/adapter-nestjs.js +39 -22
  9. package/dist/runtime/adapter-nextjs.js +38 -21
  10. package/dist/runtime/contexts/http-request-context.d.ts +3 -1
  11. package/dist/runtime/headers.js +1 -1
  12. package/dist/runtime/http.js +47 -30
  13. package/dist/runtime/stdio.js +17 -11
  14. package/dist/runtime/utils/elicitation.d.ts +4 -0
  15. package/dist/runtime/utils/ext-apps-registry.d.ts +132 -0
  16. package/dist/runtime/utils/path-to-tool-name.d.ts +20 -0
  17. package/dist/runtime/utils/prompt-loader.d.ts +11 -0
  18. package/dist/runtime/utils/prompts.d.ts +8 -0
  19. package/dist/runtime/utils/react/generate-html.d.ts +1 -0
  20. package/dist/runtime/utils/react/index.d.ts +4 -0
  21. package/dist/runtime/utils/resource-loader.d.ts +11 -0
  22. package/dist/runtime/utils/resources.d.ts +4 -0
  23. package/dist/runtime/utils/server.d.ts +35 -0
  24. package/dist/runtime/utils/tool-loader.d.ts +11 -0
  25. package/dist/runtime/utils/tools.d.ts +11 -0
  26. package/dist/runtime/utils/transformers/prompt.d.ts +32 -0
  27. package/dist/runtime/utils/transformers/resource.d.ts +26 -0
  28. package/dist/runtime/utils/transformers/tool.d.ts +30 -0
  29. package/dist/runtime/utils/ui/flatten-meta.d.ts +6 -0
  30. package/dist/runtime/utils/ui/split-meta.d.ts +17 -0
  31. package/dist/runtime/utils/utils/resource-uri-composer.d.ts +60 -0
  32. package/dist/runtime/utils/validators/index.d.ts +13 -0
  33. package/dist/types/client-info.d.ts +12 -0
  34. package/dist/types/tool.d.ts +69 -14
  35. package/dist/utils/context.d.ts +1 -1
  36. package/package.json +9 -1
  37. package/src/client/index.ts +4 -1
  38. package/src/compiler/generate-tools-code.ts +4 -6
  39. package/src/compiler/get-bundler-config/plugins/index.ts +0 -1
  40. package/src/compiler/index.ts +24 -7
  41. package/src/index.ts +8 -1
  42. package/src/runtime/adapters/express/index.ts +45 -47
  43. package/src/runtime/adapters/nestjs/xmcp.service.ts +4 -3
  44. package/src/runtime/adapters/nextjs/handler/node-to-web-adapter.ts +40 -6
  45. package/src/runtime/adapters/nextjs/handler/request-converter.ts +20 -0
  46. package/src/runtime/adapters/nextjs/handler/server-lifecycle.ts +8 -5
  47. package/src/runtime/adapters/nextjs/index.ts +41 -25
  48. package/src/runtime/contexts/http-request-context.ts +2 -0
  49. package/src/runtime/headers.ts +37 -2
  50. package/src/runtime/platforms/cloudflare/cors.ts +0 -1
  51. package/src/runtime/platforms/cloudflare/worker.ts +46 -44
  52. package/src/runtime/transports/http/index.ts +0 -4
  53. package/src/runtime/transports/http/stateless-streamable-http.ts +131 -233
  54. package/src/runtime/transports/stdio/index.ts +0 -2
  55. package/src/runtime/utils/__tests__/client-info.test.ts +80 -0
  56. package/src/runtime/utils/__tests__/tool-extra.test.ts +110 -0
  57. package/src/runtime/utils/client-info.ts +71 -0
  58. package/src/runtime/utils/elicitation.ts +396 -0
  59. package/src/runtime/utils/prompt-loader.ts +151 -0
  60. package/src/runtime/utils/resource-loader.ts +151 -0
  61. package/src/runtime/utils/resources.ts +0 -8
  62. package/src/runtime/utils/server.ts +28 -38
  63. package/src/runtime/utils/tool-loader.ts +149 -0
  64. package/src/runtime/utils/tools.ts +9 -6
  65. package/src/runtime/utils/transformers/tool.ts +26 -5
  66. package/src/types/client-info.ts +12 -0
  67. package/src/types/injected.d.ts +62 -0
  68. package/src/types/tool.ts +99 -17
  69. package/src/utils/context.ts +2 -2
@@ -0,0 +1,62 @@
1
+ // ─── ProvidePlugin (module exports wired from .xmcp/import-map.js) ────────────
2
+
3
+ declare const INJECTED_TOOLS: Record<
4
+ string,
5
+ () => Promise<import("../runtime/utils/server").ToolFile>
6
+ >;
7
+
8
+ declare const INJECTED_PROMPTS: Record<
9
+ string,
10
+ () => Promise<import("../runtime/utils/server").PromptFile>
11
+ >;
12
+
13
+ declare const INJECTED_RESOURCES: Record<
14
+ string,
15
+ () => Promise<import("../runtime/utils/server").ResourceFile>
16
+ >;
17
+
18
+ declare const INJECTED_MIDDLEWARE:
19
+ | (() => Promise<{
20
+ default?:
21
+ | import("./middleware").Middleware
22
+ | import("./middleware").Middleware[]
23
+ | import("./middleware").WebMiddleware
24
+ | import("./middleware").WebMiddleware[];
25
+ }>)
26
+ | undefined;
27
+
28
+ // ─── DefinePlugin — config objects ────────────────────────────────────────────
29
+
30
+ declare const HTTP_CONFIG: Omit<
31
+ NonNullable<import("../compiler/config").ResolvedHttpConfig>,
32
+ "cors"
33
+ >;
34
+
35
+ declare const HTTP_CORS_CONFIG: import("../compiler/config").CorsConfig;
36
+
37
+ declare const TEMPLATE_CONFIG: import("../compiler/config").TemplateConfig;
38
+
39
+ declare const STDIO_CONFIG: { debug: boolean; silent: boolean };
40
+
41
+ declare const SERVER_INFO: import("@modelcontextprotocol/sdk/types").Implementation;
42
+
43
+ // ─── DefinePlugin — individual CORS vars (legacy Express adapter) ──────────────
44
+
45
+ declare const HTTP_CORS_ORIGIN: string;
46
+ declare const HTTP_CORS_METHODS: string;
47
+ declare const HTTP_CORS_ALLOWED_HEADERS: string;
48
+ declare const HTTP_CORS_EXPOSED_HEADERS: string;
49
+ declare const HTTP_CORS_CREDENTIALS: boolean;
50
+ declare const HTTP_CORS_MAX_AGE: number;
51
+ declare const HTTP_DEBUG: boolean;
52
+ declare const HTTP_BODY_SIZE_LIMIT: string;
53
+
54
+ // ─── DefinePlugin — runtime flags ─────────────────────────────────────────────
55
+
56
+ declare const IS_CLOUDFLARE: boolean;
57
+
58
+ declare const INJECTED_CLIENT_BUNDLES:
59
+ | Record<string, { js: string; css?: string }>
60
+ | undefined;
61
+
62
+ declare const RUNTIME_FILES: Record<string, string>;
package/src/types/tool.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  import { z } from "zod/v3";
2
2
  import type { ZodType as ZodTypeV4, infer as inferV4 } from "zod";
3
+ import type { ElicitResult as McpElicitResult } from "@modelcontextprotocol/sdk/types";
3
4
  import { UIMetadata } from "./ui-meta";
5
+ import type { McpClientInfo } from "./client-info";
4
6
 
5
7
  export interface ToolAnnotations {
6
8
  /** Human-readable title for the tool */
@@ -31,11 +33,95 @@ export interface ToolMetadata {
31
33
  }
32
34
 
33
35
  type CompatibleZodType = z.ZodTypeAny | ZodTypeV4<unknown>;
36
+ type InferCompatibleZodType<T extends CompatibleZodType> =
37
+ T extends z.ZodTypeAny
38
+ ? z.infer<T>
39
+ : T extends ZodTypeV4<unknown>
40
+ ? inferV4<T>
41
+ : never;
34
42
 
35
43
  export type ToolSchema = Record<string, CompatibleZodType>;
36
44
  export type ToolOutputSchema = Record<string, CompatibleZodType>;
45
+ export type ElicitResult = McpElicitResult;
46
+
47
+ export interface ToolRequestOptions {
48
+ /** Progress notification callback */
49
+ onprogress?: (progress: any) => void;
50
+ /** Abort signal for cancelling the request */
51
+ signal?: AbortSignal;
52
+ /** Request timeout in milliseconds */
53
+ timeout?: number;
54
+ /** Whether receiving progress notifications resets the timeout */
55
+ resetTimeoutOnProgress?: boolean;
56
+ /** Maximum total time to wait for a response */
57
+ maxTotalTimeout?: number;
58
+ /** Additional transport-specific options */
59
+ [key: string]: unknown;
60
+ }
61
+
62
+ type ElicitStringFormat = "date" | "uri" | "email" | "date-time";
63
+
64
+ interface ElicitFieldBase {
65
+ title?: string;
66
+ description?: string;
67
+ }
68
+
69
+ export interface ElicitStringField extends ElicitFieldBase {
70
+ type: "string";
71
+ minLength?: number;
72
+ maxLength?: number;
73
+ format?: ElicitStringFormat;
74
+ default?: string;
75
+ }
76
+
77
+ export interface ElicitEnumField extends ElicitFieldBase {
78
+ type: "string";
79
+ enum: string[];
80
+ enumNames?: string[];
81
+ default?: string;
82
+ }
83
+
84
+ export interface ElicitBooleanField extends ElicitFieldBase {
85
+ type: "boolean";
86
+ default?: boolean;
87
+ }
37
88
 
38
- // The ToolExtraArguments type is equivalent to Parameters<ToolCallback<undefined>>[0] from @modelcontextprotocol/sdk but fully resolved to avoid external type dependencies.
89
+ export interface ElicitNumberField extends ElicitFieldBase {
90
+ type: "number" | "integer";
91
+ minimum?: number;
92
+ maximum?: number;
93
+ default?: number;
94
+ }
95
+
96
+ export type ElicitFormField =
97
+ | ElicitStringField
98
+ | ElicitEnumField
99
+ | ElicitBooleanField
100
+ | ElicitNumberField;
101
+
102
+ export interface ElicitFormSchema {
103
+ type: "object";
104
+ properties: Record<string, ElicitFormField>;
105
+ required?: string[];
106
+ }
107
+
108
+ export interface ElicitFormRequest {
109
+ mode?: "form";
110
+ message: string;
111
+ requestedSchema: ElicitFormSchema;
112
+ }
113
+
114
+ export interface ElicitUrlRequest {
115
+ mode: "url";
116
+ message: string;
117
+ url: string;
118
+ elicitationId: string;
119
+ }
120
+
121
+ export type ElicitRequest = ElicitFormRequest | ElicitUrlRequest;
122
+
123
+ // The ToolExtraArguments type is based on Parameters<ToolCallback<undefined>>[0]
124
+ // from @modelcontextprotocol/sdk, with xmcp-specific extensions.
39
125
  /**
40
126
  * Extra arguments passed to MCP tool functions.
41
127
  */
@@ -77,28 +163,24 @@ export interface ToolExtraArguments {
77
163
  headers: Record<string, string | string[] | undefined>;
78
164
  };
79
165
 
166
+ /** MCP client metadata from initialize.params.clientInfo, when available */
167
+ clientInfo?: McpClientInfo;
168
+
80
169
  /** Sends a notification that relates to the current request being handled */
81
170
  sendNotification: (notification: any) => Promise<void>;
82
171
 
83
172
  /** Sends a request that relates to the current request being handled */
84
- sendRequest: <U extends z.ZodType<object>>(
173
+ sendRequest: <U extends CompatibleZodType>(
85
174
  request: any,
86
175
  resultSchema: U,
87
- options?: {
88
- /** Progress notification callback */
89
- onprogress?: (progress: any) => void;
90
- /** Abort signal for cancelling the request */
91
- signal?: AbortSignal;
92
- /** Request timeout in milliseconds */
93
- timeout?: number;
94
- /** Whether receiving progress notifications resets the timeout */
95
- resetTimeoutOnProgress?: boolean;
96
- /** Maximum total time to wait for a response */
97
- maxTotalTimeout?: number;
98
- /** Additional transport-specific options */
99
- [key: string]: unknown;
100
- }
101
- ) => Promise<z.infer<U>>;
176
+ options?: ToolRequestOptions
177
+ ) => Promise<InferCompatibleZodType<U>>;
178
+
179
+ /** Requests user input from the connected client */
180
+ elicit: (
181
+ request: ElicitRequest,
182
+ options?: ToolRequestOptions
183
+ ) => Promise<ElicitResult>;
102
184
  }
103
185
 
104
186
  export type InferSchema<T extends Record<string, unknown>> = {
@@ -8,7 +8,7 @@ type SetContext<T extends DefaultContext> = (data: Partial<T>) => void;
8
8
 
9
9
  export interface Context<T extends DefaultContext> {
10
10
  /** Run a callback with a specific context. */
11
- provider: (initialValue: T, callback: () => void) => void;
11
+ provider: <R>(initialValue: T, callback: () => R) => R;
12
12
  /** Get the context. */
13
13
  getContext: GetContext<T>;
14
14
  /** Partially update the context. */
@@ -88,7 +88,7 @@ export function createContext<T extends Object>({
88
88
  Object.assign(store, data);
89
89
  };
90
90
 
91
- const provider = (initialValue: T, callback: () => void) => {
91
+ const provider = <R>(initialValue: T, callback: () => R): R => {
92
92
  fallbackStoreWrapper.current = initialValue;
93
93
  return context.run(initialValue, callback);
94
94
  };