diff --git a/.changeset/mcp-input-required-schema.md b/.changeset/mcp-input-required-schema.md new file mode 100644 index 0000000000..0c202eadef --- /dev/null +++ b/.changeset/mcp-input-required-schema.md @@ -0,0 +1,10 @@ +--- +"effect": minor +--- + +`McpSchema.InputRequired` is now a schema class and `McpSchema.McpInputRequest` is now a schema, so a toolkit tool can declare `failure: McpSchema.InputRequired`. + +### Migration + +- The `McpSchema.InputRequired` constructor now validates its fields and throws when they are invalid, for example for an unknown input request `method`. +- A `Schema.instanceOf(McpSchema.InputRequired)` failure schema has no JSON codec, so in `failureMode: "return"` it fails with `ToolResultEncodingError`. Declare `failure: McpSchema.InputRequired` instead. diff --git a/packages/effect/MCP.md b/packages/effect/MCP.md index f863ade80c..7d02fd263c 100644 --- a/packages/effect/MCP.md +++ b/packages/effect/MCP.md @@ -480,7 +480,7 @@ import { McpSchema, McpServer, Tool, Toolkit } from "effect/ai" const ArchiveNote = Tool.make("ArchiveNote", { parameters: Schema.Struct({ noteId: Schema.String }), success: Schema.Struct({ archived: Schema.String }), - failure: Schema.instanceOf(McpSchema.InputRequired), + failure: McpSchema.InputRequired, dependencies: [McpSchema.McpRequestContext] }) @@ -531,9 +531,10 @@ const NotesLayer = McpServer.toolkit(NotesToolkit).pipe( The first call fails with `InputRequired`, which the client receives as an `input_required` result. On the retry, the handler reads the answer and the state from `McpRequestContext`. -- `InputRequired` is not an error class, so fail with `Effect.fail(new McpSchema.InputRequired(...))`. - It can also be one member of a union failure schema. For a tool you do not own, widen its failure - with `tool.setFailure(Schema.Union([...]))` before building the toolkit. +- `InputRequired` is a schema class, not an error class. Declare it directly as the failure schema and + fail with `Effect.fail(new McpSchema.InputRequired(...))`. It can also be one member of a union + failure schema. For a tool you do not own, widen its failure with + `tool.setFailure(Schema.Union([...]))` before building the toolkit. - Protocols before `v2026_07_28` reject the result with an `InvalidParams` error ("Client input is not supported by this MCP protocol"), as they do for `addTool`. - The client must declare the capability each input request needs, such as `elicitation`. diff --git a/packages/effect/src/ai/McpSchema.ts b/packages/effect/src/ai/McpSchema.ts index ba6c8308c3..e5ffd2a161 100644 --- a/packages/effect/src/ai/McpSchema.ts +++ b/packages/effect/src/ai/McpSchema.ts @@ -3028,6 +3028,19 @@ export const ElicitResult = Schema.Union([ */ export type ElicitResult = typeof ElicitResult.Type +/** + * Version-neutral request for additional MCP client input. + * + * @stability unstable + * @category schemas + * @since 4.0.0 + */ +export const McpInputRequest = Schema.Union([ + Schema.Struct({ method: Schema.Literal("roots/list"), params: optional(UnknownObject) }), + Schema.Struct({ method: Schema.Literal("sampling/createMessage"), params: UnknownObject }), + Schema.Struct({ method: Schema.Literal("elicitation/create"), params: UnknownObject }) +]).annotate({ identifier: "McpInputRequest" }) + /** * Version-neutral request for additional MCP client input. * @@ -3035,10 +3048,7 @@ export type ElicitResult = typeof ElicitResult.Type * @category models * @since 4.0.0 */ -export type McpInputRequest = - | { readonly method: "roots/list"; readonly params?: Readonly> | undefined } - | { readonly method: "sampling/createMessage"; readonly params: Readonly> } - | { readonly method: "elicitation/create"; readonly params: Readonly> } +export type McpInputRequest = typeof McpInputRequest.Type /** * Version-neutral response supplied by an MCP client for a prior input request. @@ -3062,14 +3072,26 @@ export type McpInputResponse = Readonly> * `requestState` is opaque to the client and is returned unchanged with the * keyed input responses. * + * `InputRequired` is also a schema, so a toolkit tool can declare it as its + * `failure`. The constructor validates its fields and throws when they are + * invalid. + * * @stability unstable * @category models * @since 4.0.0 */ -export class InputRequired extends Data.TaggedClass("InputRequired")<{ - readonly inputRequests?: Readonly> | undefined - readonly requestState?: string | undefined -}> { +export class InputRequired extends Schema.TaggedClass("@effect/ai/McpSchema/InputRequired")( + "InputRequired", + Schema.Struct({ + inputRequests: optional(Schema.Record(Schema.String, McpInputRequest)), + requestState: optional(Schema.String) + }).check( + Schema.makeFilter( + (fields) => fields.inputRequests !== undefined || fields.requestState !== undefined, + { message: "InputRequired requires at least one of inputRequests or requestState" } + ) + ) +) { // The overload preserves the requirement that at least one of inputRequests or requestState is present. // oxlint-disable-next-line no-useless-constructor constructor( @@ -3081,9 +3103,10 @@ export class InputRequired extends Data.TaggedClass("InputRequired")<{ | { readonly inputRequests?: Readonly> | undefined readonly requestState: string - } + }, + options?: Schema.MakeOptions ) { - super(fields) + super(fields, options) } } diff --git a/packages/effect/test/ai/McpServer/ProtocolAdapters.test.ts b/packages/effect/test/ai/McpServer/ProtocolAdapters.test.ts index f8265f135b..f912329e3b 100644 --- a/packages/effect/test/ai/McpServer/ProtocolAdapters.test.ts +++ b/packages/effect/test/ai/McpServer/ProtocolAdapters.test.ts @@ -99,14 +99,14 @@ class RootsUnavailable extends Schema.TaggedError()("RootsUnav const InputRequiredTool = Tool.make("input-required", { parameters: Tool.EmptyParams, success: Schema.String, - failure: Schema.instanceOf(McpSchema.InputRequired), + failure: McpSchema.InputRequired, dependencies: [McpSchema.McpRequestContext] }) const InputRequiredReturnTool = Tool.make("input-required-return", { parameters: Tool.EmptyParams, success: Schema.String, - failure: Schema.Union([Schema.instanceOf(McpSchema.InputRequired), RootsUnavailable]), + failure: Schema.Union([McpSchema.InputRequired, RootsUnavailable]), failureMode: "return", dependencies: [McpSchema.McpRequestContext] }) diff --git a/packages/effect/typetest/ai/McpServer.tst.ts b/packages/effect/typetest/ai/McpServer.tst.ts index 6379f97fbb..aeac4e1c6e 100644 --- a/packages/effect/typetest/ai/McpServer.tst.ts +++ b/packages/effect/typetest/ai/McpServer.tst.ts @@ -210,7 +210,7 @@ describe("McpServer", () => { }) it("should accept InputRequired as a declared toolkit failure", () => { - const toolkit = Toolkit.make(Tool.make("Confirm", { failure: Schema.instanceOf(McpSchema.InputRequired) })) + const toolkit = Toolkit.make(Tool.make("Confirm", { failure: McpSchema.InputRequired })) const layer = McpServer.toolkit(toolkit).pipe( Layer.provide(toolkit.toLayer({ Confirm: () => Effect.fail(new McpSchema.InputRequired({ requestState: "state" }))