|
| 1 | +import { z } from "zod"; |
| 2 | +import { toolsMetadata } from "../config.js"; |
| 3 | +import { CommonProjectsInput } from "../schemas.js"; |
| 4 | +import { respondWithError, toolHandler } from "../utils.js"; |
| 5 | + |
| 6 | +const SESSION_CHANNEL_NAME_REGEX = /^[A-Za-z0-9._-]{1,128}$/; |
| 7 | + |
| 8 | +const ReadSessionChannelInput = CommonProjectsInput.extend({ |
| 9 | + sessionId: z |
| 10 | + .string() |
| 11 | + .describe("The session id (session_* friendlyId) or the externalId it was created with."), |
| 12 | + channel: z |
| 13 | + .string() |
| 14 | + .describe( |
| 15 | + "The named side channel to read. Omit to read the session's reserved chat transcript pair." |
| 16 | + ) |
| 17 | + .optional(), |
| 18 | + io: z |
| 19 | + .enum(["out", "in"]) |
| 20 | + .describe("Which side to read: `out` (producer feed) or `in` (client input).") |
| 21 | + .default("out"), |
| 22 | + afterEventId: z |
| 23 | + .string() |
| 24 | + .describe( |
| 25 | + "Cursor: only return records after this event id. Use the nextCursor from a prior read." |
| 26 | + ) |
| 27 | + .optional(), |
| 28 | + maxRecords: z |
| 29 | + .number() |
| 30 | + .int() |
| 31 | + .positive() |
| 32 | + .max(500) |
| 33 | + .describe("Maximum records to return (default 100).") |
| 34 | + .default(100), |
| 35 | +}); |
| 36 | + |
| 37 | +export const readSessionChannelTool = { |
| 38 | + name: toolsMetadata.read_session_channel.name, |
| 39 | + title: toolsMetadata.read_session_channel.title, |
| 40 | + description: toolsMetadata.read_session_channel.description, |
| 41 | + inputSchema: ReadSessionChannelInput.shape, |
| 42 | + handler: toolHandler(ReadSessionChannelInput.shape, async (input, { ctx }) => { |
| 43 | + ctx.logger?.log("calling read_session_channel", { input }); |
| 44 | + |
| 45 | + if (ctx.options.devOnly && input.environment !== "dev") { |
| 46 | + return respondWithError(`This MCP server is only available for the dev environment.`); |
| 47 | + } |
| 48 | + |
| 49 | + if (input.channel !== undefined && !SESSION_CHANNEL_NAME_REGEX.test(input.channel)) { |
| 50 | + return respondWithError( |
| 51 | + `Invalid channel name "${input.channel}": use 1-128 chars from [A-Za-z0-9._-].` |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + const projectRef = await ctx.getProjectRef({ |
| 56 | + projectRef: input.projectRef, |
| 57 | + cwd: input.configPath, |
| 58 | + }); |
| 59 | + |
| 60 | + const apiClient = await ctx.getApiClient({ |
| 61 | + projectRef, |
| 62 | + environment: input.environment, |
| 63 | + scopes: ["read:sessions"], |
| 64 | + branch: input.branch, |
| 65 | + }); |
| 66 | + |
| 67 | + const { records } = await apiClient.readSessionStreamRecords(input.sessionId, input.io, { |
| 68 | + channel: input.channel, |
| 69 | + afterEventId: input.afterEventId, |
| 70 | + }); |
| 71 | + |
| 72 | + const limited = records.slice(0, input.maxRecords); |
| 73 | + const hasMore = records.length > limited.length; |
| 74 | + const nextCursor = limited.at(-1)?.seqNum; |
| 75 | + |
| 76 | + const label = input.channel ? `channel "${input.channel}"` : "reserved pair"; |
| 77 | + const header = `Session ${input.sessionId} ${label} .${input.io}: ${limited.length} record${ |
| 78 | + limited.length === 1 ? "" : "s" |
| 79 | + }${hasMore ? ` (more available)` : ""}`; |
| 80 | + |
| 81 | + const lines = limited.map((record) => { |
| 82 | + const data = typeof record.data === "string" ? record.data : JSON.stringify(record.data); |
| 83 | + return `#${record.seqNum} ${data}`; |
| 84 | + }); |
| 85 | + |
| 86 | + const footer = |
| 87 | + nextCursor !== undefined && hasMore |
| 88 | + ? `\n\nMore records available. Read again with afterEventId "${nextCursor}" to continue.` |
| 89 | + : ""; |
| 90 | + |
| 91 | + return { |
| 92 | + content: [ |
| 93 | + { |
| 94 | + type: "text", |
| 95 | + text: [header, "", ...lines].join("\n") + footer, |
| 96 | + }, |
| 97 | + ], |
| 98 | + }; |
| 99 | + }), |
| 100 | +}; |
| 101 | + |
| 102 | +const WriteSessionChannelInput = CommonProjectsInput.extend({ |
| 103 | + sessionId: z |
| 104 | + .string() |
| 105 | + .describe("The session id (session_* friendlyId) or the externalId it was created with."), |
| 106 | + channel: z.string().describe("The named side channel to write to."), |
| 107 | + value: z |
| 108 | + .string() |
| 109 | + .describe( |
| 110 | + "The record to append to the channel's `in` stream. Pass a JSON string for structured records (e.g. '{\"paused\":true}')." |
| 111 | + ), |
| 112 | +}); |
| 113 | + |
| 114 | +export const writeSessionChannelTool = { |
| 115 | + name: toolsMetadata.write_session_channel.name, |
| 116 | + title: toolsMetadata.write_session_channel.title, |
| 117 | + description: toolsMetadata.write_session_channel.description, |
| 118 | + inputSchema: WriteSessionChannelInput.shape, |
| 119 | + handler: toolHandler(WriteSessionChannelInput.shape, async (input, { ctx }) => { |
| 120 | + ctx.logger?.log("calling write_session_channel", { input }); |
| 121 | + |
| 122 | + if (ctx.options.devOnly && input.environment !== "dev") { |
| 123 | + return respondWithError(`This MCP server is only available for the dev environment.`); |
| 124 | + } |
| 125 | + |
| 126 | + if (!SESSION_CHANNEL_NAME_REGEX.test(input.channel)) { |
| 127 | + return respondWithError( |
| 128 | + `Invalid channel name "${input.channel}": use 1-128 chars from [A-Za-z0-9._-].` |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + const projectRef = await ctx.getProjectRef({ |
| 133 | + projectRef: input.projectRef, |
| 134 | + cwd: input.configPath, |
| 135 | + }); |
| 136 | + |
| 137 | + const apiClient = await ctx.getApiClient({ |
| 138 | + projectRef, |
| 139 | + environment: input.environment, |
| 140 | + scopes: ["write:sessions"], |
| 141 | + branch: input.branch, |
| 142 | + }); |
| 143 | + |
| 144 | + await apiClient.appendToSessionStream( |
| 145 | + input.sessionId, |
| 146 | + "in", |
| 147 | + input.value, |
| 148 | + undefined, |
| 149 | + input.channel |
| 150 | + ); |
| 151 | + |
| 152 | + return { |
| 153 | + content: [ |
| 154 | + { |
| 155 | + type: "text", |
| 156 | + text: `Wrote 1 record to session ${input.sessionId} channel "${input.channel}" .in. This does not wake or trigger a run.`, |
| 157 | + }, |
| 158 | + ], |
| 159 | + }; |
| 160 | + }), |
| 161 | +}; |
0 commit comments