Open History to see the last commit
1/**
2 * Thrown when an MCP call needs input outside `chat()`.
3 *
4 * `kind` is `form` for user input, or `sampling` for a model request.
5 * `request` is the MCP input request body.
6 * The `name` is always `MCPInputRequiredError`.
7 * Another package can read this shape without an import of this class.
8 *
9 * @param kind - `form` for user input, or `sampling` for a model request
10 * @param request - The MCP input request body
11 *
12 * @example
13 * throw new MCPInputRequiredError('form', { message: 'Which city?' })
14 */
15export class MCPInputRequiredError extends Error {
16 constructor(
17 public readonly kind: 'form' | 'sampling',
18 public readonly request: unknown,
19 ) {
20 super('The MCP server asked for input. Retry the call with the result.')
21 this.name = 'MCPInputRequiredError'
22 }
23}
24
25/**
26 * This function returns true when `value` has the input-required error shape.
27 *
28 * The check reads `name`, `kind`, and `request`.
29 * A plain object with those fields passes.
30 * The check does not use `instanceof`.
31 *
32 * @param value - A thrown value, or a plain object
33 *
34 * @example
35 * if (isMCPInputRequiredError(error)) {
36 * error.kind
37 * }
38 */
39export function isMCPInputRequiredError(
40 value: unknown,
41): value is MCPInputRequiredError {
42 if (typeof value !== 'object' || value === null) return false
43 if (!('name' in value) || value.name !== 'MCPInputRequiredError') {
44 return false
45 }
46 if (!('kind' in value)) return false
47
48 const kindIsFormOrSampling =
49 value.kind === 'form' || value.kind === 'sampling'
50 if (!kindIsFormOrSampling) return false
51
52 return 'request' in value
53}