mcp-extras.ts
This file
Path
packages/protocol/src/mcp-extras.ts
223 lines · 6.2 KiB · TypeScript
Last changed by
Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f
History of this fileTake it
The walk stopped after 200 commits: lines older than 678c8b8 point at it.
Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 1–7why, one step back1import { Brand } from "@repo/brand";2import { DateTime, Effect, Layer, Option, Schema } from "effect";3import { McpSchema, McpServer } from "effect/ai";45import { Client } from "./contracts.ts";6import type { AskListing } from "./domain/ask.ts";7import type { ProjectListing } from "./domain/front-page.ts";Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
line 8why, one step back8import { AskId, keyOf, ProjectName, ProjectNameFromKey } from "./ids.ts";Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 9–13why, one step back9import { Refusal } from "./refusal.ts";10import { explain } from "./render.ts";1112const MARKDOWN = "text/markdown";13Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f
lines 14–15why, one step back14// By the project's key: a resource template's parameter is one segment, and15// a name has two or three.Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
line 16why, one step back16const briefUri = (project: ProjectName, ask: AskId) =>Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
line 17why, one step back17 `${Brand.urlScheme}ask/${keyOf(project)}/${ask}`;Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 18–33why, one step back1819const projectsUri = `${Brand.urlScheme}projects`;2021const markdown = (uri: string, text: string) => ({22 contents: [{ mimeType: MARKDOWN, text, uri }],23});2425const isRefusal = Schema.is(Refusal);2627const refusedAsText = <A, E, R>(read: Effect.Effect<A, E, R>) =>28 Effect.catchIf(read, isRefusal, (refusal) =>29 Effect.fail(30 new McpSchema.InternalError({ message: explain([refusal]).join("\n") })31 )32 );33Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f
line 34why, one step back34// Every cell's schema already keeps out `|` and line breaks, so none is escaped.Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 35–59why, one step back35const projectsTable = (projects: readonly ProjectListing[]) =>36 projects.length === 037 ? "No projects yet."38 : [39 "| project | posture | open asks | landed |",40 "| --- | --- | --- | --- |",41 ...projects.map(42 ({ landedAt, openAsks, posture, project }) =>43 `| ${[44 project,45 posture,46 String(openAsks),47 landedAt === undefined ? "never" : DateTime.formatIso(landedAt),48 ].join(" | ")} |`49 ),50 ].join("\n");5152const briefParts = [`${Brand.urlScheme}ask/`, "/", ""];5354const briefTemplate: TemplateStringsArray = Object.assign(briefParts, {55 raw: briefParts,56});5758const askBrief = McpServer.resource(59 briefTemplate,Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
line 60why, one step back60 McpSchema.param("project", ProjectNameFromKey),Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 61–87why, one step back61 McpSchema.param("ask", AskId)62)({63 content: (uri, project, ask) =>64 Effect.gen(function* () {65 const client = yield* Client;66 const { brief } = yield* client.showAsk({ ask, project });6768 return markdown(uri, brief);69 }).pipe(refusedAsText),70 description:71 "An ask's brief, in Markdown: what an answerer reads before claiming it.",72 mimeType: MARKDOWN,73 name: "ask brief",74});7576const projectList = McpServer.resource({77 content: Effect.gen(function* () {78 const client = yield* Client;79 const { projects } = yield* client.listProjects({});8081 return markdown(82 projectsUri,83 [84 "# Projects",85 "",86 projectsTable(projects),87 "",Merge build/1413-docs-and-texts into mainoscabrielbefore adoptionf10338e
line 88why, one step back88 `A project's open asks: the prompt \`contribute\` with its name, or \`${Brand.cli} asks --project <owner>/<name>\`.`,Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 89–223why, one step back89 "",90 ].join("\n")91 );92 }).pipe(refusedAsText),93 description:94 "Every project on the platform, with its posture and how many asks it has open.",95 mimeType: MARKDOWN,96 name: "projects",97 uri: projectsUri,98});99100export const briefResources: Layer.Layer<never, never, Client> = Layer.mergeAll(101 askBrief,102 projectList103);104105export interface Skill {106 readonly name: string;107 readonly description: string;108 readonly body: string;109}110111const OPEN = "---\n";112113const CLOSE = "\n---\n";114115const decodeQuoted = Schema.decodeUnknownOption(116 Schema.fromJsonString(Schema.String)117);118119const scalar = (raw: string) =>120 raw.startsWith('"') ? decodeQuoted(raw) : Option.some(raw);121122const fieldsOf = (head: string) =>123 new Map(124 head.split("\n").flatMap((line) => {125 const { key, raw } =126 /^(?<key>[a-z-]+): (?<raw>.+)$/u.exec(line)?.groups ?? {};127128 return key === undefined || raw === undefined ? [] : [[key, raw]];129 })130 );131132const skillOf = (file: string): Option.Option<Skill> => {133 const close = file.startsWith(OPEN)134 ? file.indexOf(CLOSE, OPEN.length - 1)135 : -1;136137 if (close === -1) {138 return Option.none();139 }140141 const fields = fieldsOf(file.slice(OPEN.length, close));142143 const field = (key: string) =>144 Option.flatMap(Option.fromNullishOr(fields.get(key)), scalar);145146 return Option.all({147 body: Option.some(file.slice(close + CLOSE.length)),148 description: field("description"),149 name: field("name"),150 });151};152153export const skillIn = <E, R>(154 generated: Effect.Effect<readonly { readonly content: string }[], E, R>155): Effect.Effect<Skill, E, R> =>156 Effect.flatMap(generated, ([file]) =>157 Option.match(158 Option.flatMap(Option.fromNullishOr(file), ({ content }) =>159 skillOf(content)160 ),161 {162 onNone: () =>163 Effect.die("the generator made no SKILL.md with its frontmatter"),164 onSome: Effect.succeed,165 }166 )167 );168169const askLines =170 (project: ProjectName) =>171 ({ ask, blockedBy, claimed, kind, territory, want }: AskListing) => {172 const notes = [173 kind,174 `in ${territory}`,175 ...(claimed ? ["claimed"] : []),176 ...(blockedBy.length === 0 ? [] : [`waits on ${blockedBy.join(", ")}`]),177 ];178179 return [180 `- \`${ask}\` (${notes.join("; ")}): ${want}`,181 ` Brief: \`${briefUri(project, ask)}\``,182 ];183 };184185const openAsks = (project: ProjectName) =>186 Effect.gen(function* () {187 const client = yield* Client;188 const { asks } = yield* client.listAsks({ project });189190 return [191 `## Open asks in ${project}`,192 "",193 ...(asks.length === 0194 ? [`${project} has no open asks.`]195 : [196 ...asks.flatMap(askLines(project)),197 "",198 `Claim one with \`${Brand.cli} claim <ask> --project ${project}\`.`,199 ]),200 "",201 ].join("\n");202 });203204export const skillPrompt = (skill: Skill): Layer.Layer<never, never, Client> =>205 McpServer.prompt({206 content: ({ project }) =>207 project === undefined208 ? Effect.succeed(skill.body)209 : openAsks(project).pipe(210 Effect.map((section) => `${skill.body}\n${section}`),211 refusedAsText212 ),213 description: skill.description,214 name: "contribute",215 parameters: {216 project: Schema.optionalKey(217 ProjectName.annotate({218 description: "A project whose open asks to list after the skill",219 })220 ),221 },222 title: `Contribute (the ${skill.name} skill)`,223 });