mirror of
https://github.com/Effect-TS/effect.git
synced 2026-12-06 03:59:09 +08:00
Make McpSchema.InputRequired a schema class (#10368)
This commit is contained in:
@@ -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.
|
||||
@@ -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`.
|
||||
|
||||
@@ -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<Record<string, unknown>> | undefined }
|
||||
| { readonly method: "sampling/createMessage"; readonly params: Readonly<Record<string, unknown>> }
|
||||
| { readonly method: "elicitation/create"; readonly params: Readonly<Record<string, unknown>> }
|
||||
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<Record<string, unknown>>
|
||||
* `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<Record<string, McpInputRequest>> | undefined
|
||||
readonly requestState?: string | undefined
|
||||
}> {
|
||||
export class InputRequired extends Schema.TaggedClass<InputRequired>("@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<Record<string, McpInputRequest>> | undefined
|
||||
readonly requestState: string
|
||||
}
|
||||
},
|
||||
options?: Schema.MakeOptions
|
||||
) {
|
||||
super(fields)
|
||||
super(fields, options)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -99,14 +99,14 @@ class RootsUnavailable extends Schema.TaggedError<RootsUnavailable>()("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]
|
||||
})
|
||||
|
||||
@@ -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" }))
|
||||
|
||||
Reference in New Issue
Block a user