Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

mcpApps.ts

99 lines3.2 KB

1import * as Schema from "effect/Schema";
2​
3import { ThreadId, TrimmedNonEmptyString, TurnItemId } from "./baseSchemas.ts";
4​
5/**
6 * Each names the tool call that produced the app; the environment resolves the
7 * app's server from that item, so an app can only reach the server it came from.
8 */
9const McpAppTarget = {
10 threadId: ThreadId,
11 itemId: TurnItemId,
12};
13​
14export const McpAppCallToolInput = Schema.Struct({
15 ...McpAppTarget,
16 name: TrimmedNonEmptyString,
17 arguments: Schema.Record(Schema.String, Schema.Unknown),
18});
19export type McpAppCallToolInput = typeof McpAppCallToolInput.Type;
20​
21export const McpAppCallToolResult = Schema.Struct({
22 content: Schema.Array(Schema.Unknown),
23 structuredContent: Schema.optional(Schema.Unknown),
24 isError: Schema.optional(Schema.Boolean),
25 _meta: Schema.optional(Schema.Unknown),
26});
27export type McpAppCallToolResult = typeof McpAppCallToolResult.Type;
28​
29export const McpAppToolInfoInput = Schema.Struct({
30 ...McpAppTarget,
31 name: TrimmedNonEmptyString,
32});
33export type McpAppToolInfoInput = typeof McpAppToolInfoInput.Type;
34​
35export const McpAppToolInfo = Schema.Struct({
36 callable: Schema.Boolean,
37 readOnly: Schema.Boolean,
38 title: Schema.optional(Schema.String),
39 tool: Schema.optional(Schema.Unknown),
40});
41export type McpAppToolInfo = typeof McpAppToolInfo.Type;
42​
43export const McpAppReadResourceInput = Schema.Struct({
44 ...McpAppTarget,
45 uri: TrimmedNonEmptyString,
46});
47export type McpAppReadResourceInput = typeof McpAppReadResourceInput.Type;
48​
49export const McpAppReadResourceResult = Schema.Struct({
50 contents: Schema.Array(Schema.Unknown),
51});
52export type McpAppReadResourceResult = typeof McpAppReadResourceResult.Type;
53​
54/**
55 * Each update replaces the app's previous one; no content clears it.
56 */
57export const McpAppUpdateModelContextInput = Schema.Struct({
58 ...McpAppTarget,
59 conversationThreadId: ThreadId,
60 content: Schema.optional(Schema.Array(Schema.Unknown)),
61 structuredContent: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
62});
63export type McpAppUpdateModelContextInput = typeof McpAppUpdateModelContextInput.Type;
64​
65export const McpAppRequestErrorReason = Schema.Literals([
66 "not-an-app",
67 "provider-unsupported",
68 "session-stopped",
69 "tool-not-callable",
70 "unsupported-content",
71 "request-failed",
72]);
73export type McpAppRequestErrorReason = typeof McpAppRequestErrorReason.Type;
74​
75export class McpAppRequestError extends Schema.TaggedError<McpAppRequestError>()(
76 "McpAppRequestError",
77 {
78 threadId: ThreadId,
79 reason: McpAppRequestErrorReason,
80 cause: Schema.optional(Schema.Defect()),
81 },
82) {
83 override get message(): string {
84 switch (this.reason) {
85 case "not-an-app":
86 return "This tool call has no MCP app.";
87 case "provider-unsupported":
88 return "This thread's provider cannot run MCP app requests.";
89 case "session-stopped":
90 return "The app's thread is not running. Send a message in the thread that created it to use the app again.";
91 case "tool-not-callable":
92 return "This app cannot call that tool.";
93 case "unsupported-content":
94 return "Only text and structured content are supported.";
95 case "request-failed":
96 return "The app's MCP server request failed.";
97 }
98 }