Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions changes/fix-mcp-serve-no-profiles.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
### Fixed

- **`<tool> mcp serve --profiles all` starts on a computer with no profile yet.** It answers the handshake and lists
the same tools a profile's own server lists (`<tool>_tools_search`, `<tool>_read`, and `<tool>_write` where a
command writes), each taking `profile`, and every call is refused with a `configuration_error` that names the
command to set up the first profile: `<tool> personal setup`, `<tool> personal session start` for a CLI that signs
in with a session, or `<tool> personal …` for one with neither. Before, it exited before the handshake, so
directories such as Glama, which start the server in an empty container to read its tools, marked WireCat servers
as not deployable. Nothing picks or makes a profile: which tools are listed never follows a profile's permissions,
so the list is built from the program itself. `mcp serve` with no profile named, and `--profiles` with a name that
does not exist, are still refused.
24 changes: 24 additions & 0 deletions src/cli/app-mcp-command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,30 @@ describe("appMcpCommand", () => {
expect(stderr.join("")).toContain("personal, work")
})

it("**--profiles all with no profile still lists a profile's tools**, and refuses each call with how to set one up", async () => {
const own = await serve(["work", "mcp", "serve"], [{ id: 2, method: "tools/list" }])
const empty = home()
writeFileSync(join(empty.CAL_CONFIG_DIR, "config.json"), JSON.stringify({ profiles: {} }))
const { code, result } = await serve(
["mcp", "serve", "--profiles", "all"],
[{ id: 2, method: "tools/list" }, call(3, "cal_read", "events list")],
empty,
)
expect(code).toBe(0)
type Listed = { name: string; inputSchema: { properties: Record<string, unknown> } }
const tools = (result(2)?.tools ?? []) as Listed[]
const withoutProfile = tools.map(({ inputSchema: { properties, ...schema }, ...one }) => {
const { profile, ...rest } = properties
return {
...one,
inputSchema: { ...schema, properties: rest, required: (schema as { required: string[] }).required.slice(1) },
}
})
expect(withoutProfile).toEqual(own.result(2)?.tools)
expect(JSON.stringify(result(3))).toContain("no cal profile on this computer yet")
expect(result(3)).toMatchObject({ isError: true })
})

it("**ends at once when it is stopped before it listens**, with stdin still open", async () => {
const stdin = new PassThrough()
const stdout = new PassThrough()
Expand Down
64 changes: 57 additions & 7 deletions src/cli/messenger/mcp-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { type ChildProcessWithoutNullStreams, spawn } from "node:child_process"
import { realpathSync } from "node:fs"
import { join } from "node:path"
import type { Readable, Writable } from "node:stream"
import type { Tool } from "@modelcontextprotocol/server"
import { CliError, resolvePaths, visibleControls } from "@wirecat/cli-base"
import { annotate } from "@wirecat/cli-base/commands"
import { annotate, describeProgram } from "@wirecat/cli-base/commands"
import { installerOf } from "@wirecat/cli-base/update"
import { Command } from "commander"
import { ATTACHMENT_LIMIT_ENV } from "../../attachments/limits.js"
Expand All @@ -13,7 +14,7 @@ import type { HttpOptions } from "../../mcp/http/serve.js"
import { httpLockFile } from "../../mcp/http/state.js"
import { type AppIdentity, envName } from "../../ports/app.js"
import { type BaseContext, type BaseEnvironment, environmentOf } from "../context.js"
import { grantsAuthority, LOCAL_READ, LOCAL_WRITE, REMOTE_WRITE } from "../effects.js"
import { grantsAuthority, LOCAL_READ, LOCAL_WRITE, REMOTE_WRITE, readsOverMcpAt } from "../effects.js"
import { itemOf, renderAnswer } from "../envelope.js"
import { MCP_HTTP_DEFAULT_PORT, mcpServerCommand } from "../mcp-server-command.js"
import { rootOf } from "../profile.js"
Expand Down Expand Up @@ -510,11 +511,12 @@ const serveProfiles = async (self: Command, context: BaseContext, host: McpHost,
)
// `all` reads the profiles again for each session, so one set up while the server runs appears.
const profiles = () => set.profiles().filter((profile) => state(profile) !== "bot")
if (profiles().length === 0)
throw new CliError(
"configuration_error",
`no ${app.command} profile on this computer yet — set one up, then run \`${app.command} mcp serve --profiles all\` again`,
)
// A directory starts the server in an empty container only to list its tools: it starts, and each call says how
// to set up a profile. Nothing here picks or makes one.
const setup =
`no ${app.command} profile on this computer yet — set one up with \`${firstProfileCommand(self, app)}\` ` +
"(any name in place of personal), then connect again"
let catalogue: Promise<Tool[]> | undefined
const given = environmentOf<McpEnvironment>(self).mcp ?? {}
const execPath = given.execPath ?? process.execPath
const scriptPath = given.scriptPath ?? realpathSync(process.argv[1] ?? "")
Expand All @@ -529,6 +531,14 @@ const serveProfiles = async (self: Command, context: BaseContext, host: McpHost,
`profile ${profile} is not signed in — run \`${app.command} ${profile} setup\`, or sign in from WireCat Desktop, ` +
"then try again. If the owner did not choose this profile, ask which one.",
child: (profile: string) => ({ command: execPath, args: [scriptPath, profile, "mcp", "serve", ...tightened], env }),
catalogue: () => {
catalogue ??= catalogueOf(self, app)
catalogue.catch(() => {
catalogue = undefined
})
return catalogue
},
setup,
instructions: await host.instructions(profiles()),
note: (line: string) => context.renderer.note(line),
}
Expand Down Expand Up @@ -579,6 +589,46 @@ const serveProfiles = async (self: Command, context: BaseContext, host: McpHost,
})
}

/** `tg personal setup`, or `notion personal session start` for a CLI that signs in with a session. */
const firstProfileCommand = (self: Command, app: AppIdentity): string => {
const root = rootOf(self)
if (root.commands.some((one) => one.name() === "setup")) return `${app.command} personal setup`
const session = root.commands.find((one) => one.name() === "session")
if (session?.commands.some((one) => one.name() === "start")) return `${app.command} personal session start`
return `${app.command} personal …`
}

/**
* The tools a profile's server lists, built in this process from the program: which tools a server lists never
* follows a profile's permissions (they decide each command's availability inside the search), so no profile is
* needed, read or made.
*/
const catalogueOf = async (self: Command, app: AppIdentity): Promise<Tool[]> => {
const [{ McpServer }, { registerCommands }, { toolsFromCommands }, { listedTools }] = await Promise.all([
import("@modelcontextprotocol/server"),
import("../../mcp/surface.js"),
import("../../mcp/agent.js"),
import("../../mcp/all-profiles.js"),
])
const root = rootOf(self)
const refuse = async (): Promise<never> => {
throw new CliError("configuration_error", `no ${app.command} profile on this computer yet`)
}
const commands = Object.fromEntries(
toolsFromCommands(describeProgram(root), {
includeUnavailable: true,
exclude: [["bot"], ["mcp"]],
fetchOnly: readsOverMcpAt(root),
}).map((one) => [
one.command,
{ title: one.title, description: one.description, writes: one.writes, input: one.input, run: refuse },
]),
)
const server = new McpServer({ name: app.command, version: app.version }, { capabilities: { tools: {} } })
registerCommands(server, app.command, commands, app.errors, undefined, app.messenger)
return listedTools(server)
}

const TAIL_LINES = 20
const TAIL_BYTES = 2_000
const TAIL_WAIT_MS = 500
Expand Down
76 changes: 63 additions & 13 deletions src/cli/messenger/mcp-every-profile.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs"
import { existsSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs"
import { tmpdir } from "node:os"
import { join } from "node:path"
import { PassThrough } from "node:stream"
Expand Down Expand Up @@ -40,6 +40,7 @@ const serve = async (
argv = ["mcp", "serve", "--profiles", "all"],
profileState = undefined as Messenger["profileState"],
extra = {} as Record<string, string>,
config = true,
} = {},
) => {
const root = mkdtempSync(join(tmpdir(), "mcp-every-"))
Expand All @@ -49,14 +50,16 @@ const serve = async (
MESSAGING_STORE: join(root, "m.db"),
...extra,
}
mkdirSync(env.CHAT_CONFIG_DIR, { recursive: true })
writeFileSync(
join(env.CHAT_CONFIG_DIR, "config.json"),
JSON.stringify({
profiles: Object.fromEntries(profiles.map((one) => [one, {}])),
bot: { profiles: Object.fromEntries(bots.map((one) => [one, {}])) },
}),
)
if (config) {
mkdirSync(env.CHAT_CONFIG_DIR, { recursive: true })
writeFileSync(
join(env.CHAT_CONFIG_DIR, "config.json"),
JSON.stringify({
profiles: Object.fromEntries(profiles.map((one) => [one, {}])),
bot: { profiles: Object.fromEntries(bots.map((one) => [one, {}])) },
}),
)
}
const { script, log } = fakeProfileServer()
const stdin = new PassThrough()
const stdout = new PassThrough()
Expand Down Expand Up @@ -86,7 +89,7 @@ const serve = async (
.split("\n")
.filter(Boolean)
.map((line) => JSON.parse(line) as { id: number; result: Record<string, unknown> })
return { answers, log: () => readFileSync(log, "utf8"), failure, stderr: streams.stderr }
return { answers, log: () => readFileSync(log, "utf8"), failure, stderr: streams.stderr, env }
}

const HANDSHAKE = [
Expand Down Expand Up @@ -239,8 +242,55 @@ describe("chat mcp serve --profiles", () => {
expect(failure?.message).toContain("news is a bot profile")
})

it("says so when there is no profile on this computer yet", async () => {
const { failure } = await serve(HANDSHAKE, { profiles: [] })
expect(failure?.message ?? "").toContain("no chat profile on this computer yet")
it.each([
["an empty computer", { config: false }],
["a config with no profile", { profiles: [] }],
["only bot profiles", { profiles: [], bots: ["news"] }],
])(
"--profiles all on %s still starts: it lists every tool, and each call says how to set up a profile",
async (_, given) => {
const { answers, failure, log, env } = await serve(
[
...HANDSHAKE,
{ id: 2, method: "tools/list" },
{ id: 3, method: "tools/call", params: { name: "chat_read", arguments: { command: "inbox" } } },
{ id: 4, method: "tools/call", params: { name: "chat_tools_search", arguments: { profile: "personal" } } },
],
given,
)
expect(failure).toBeUndefined()
const [hello, listed, ...calls] = [1, 2, 3, 4].map((id) => answers.find((one) => one.id === id)?.result)
expect(String(hello?.instructions)).toMatch(/^No chat profile on this computer yet/)
const tools = (listed?.tools ?? []) as { name: string; inputSchema: { properties: object; required: string[] } }[]
expect(tools.map((one) => one.name)).toEqual(["chat_tools_search", "chat_read", "chat_write"])
for (const one of tools) {
expect(one.inputSchema.required[0]).toBe("profile")
expect(one.inputSchema.properties).toMatchObject({ profile: { type: "string" } })
expect(JSON.stringify(one.inputSchema)).not.toContain('"enum":[]')
}
for (const call of calls) {
expect(call).toMatchObject({ isError: true })
const text = JSON.stringify(call)
expect(text).toContain("configuration_error")
expect(text).toContain(
"no chat profile on this computer yet — set one up with `chat personal …` (any name in place of personal), then connect again",
)
expect(text).not.toContain(" at ")
}
expect(log()).toBe("")
const written = existsSync(join(env.CHAT_CONFIG_DIR, "config.json"))
? readFileSync(join(env.CHAT_CONFIG_DIR, "config.json"), "utf8")
: "{}"
expect(JSON.parse(written).profiles ?? {}).toEqual({})
expect(existsSync(env.CHAT_STATE_DIR)).toBe(false)
expect(existsSync(env.MESSAGING_STORE)).toBe(false)
},
)

it("still refuses --profiles naming a profile that does not exist, on a computer with none", async () => {
const { failure, answers } = await serve(HANDSHAKE, { config: false, argv: ["mcp", "serve", "--profiles", "work"] })
expect(failure).toMatchObject({ code: "validation_error" })
expect(failure?.message).toContain('no profile named "work" — profiles here: none')
expect(answers).toEqual([])
})
})
84 changes: 69 additions & 15 deletions src/mcp/all-profiles.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type ChildProcessWithoutNullStreams, spawn } from "node:child_process"
import { randomUUID } from "node:crypto"
import type { Readable, Writable } from "node:stream"
import { type CallToolResult, McpServer, type Tool } from "@modelcontextprotocol/server"
import { type CallToolResult, InMemoryTransport, McpServer, type Tool } from "@modelcontextprotocol/server"
import { StdioServerTransport, serveStdio } from "@modelcontextprotocol/server/stdio"
import { CliError, type ErrorCode, errorObjectOf, mcpErrorResult } from "@wirecat/cli-base"
import { CALLER_META_KEY, callerOf, PROXY_CLIENT_NAME } from "./caller.js"
Expand Down Expand Up @@ -32,6 +32,10 @@ export interface AllProfilesOptions {
/** How to sign a profile in, for the refusal of a call it cannot serve yet. */
signIn?: (profile: string) => string
child: (profile: string) => ChildSpec
/** The tools a profile's server would list, for a session that starts before any profile exists. */
catalogue?: () => Promise<readonly Tool[]>
/** How to set up the first profile: every call of a session with no profile is refused with it. */
setup?: string
instructions: string
idleMs?: number
/** A child's stderr, line by line; never this process's stdout, which carries the protocol. */
Expand Down Expand Up @@ -195,15 +199,19 @@ export const withProfile = (
inputSchema: {
...tool.inputSchema,
properties: {
profile: {
type: "string",
enum: [...profiles],
oneOf: profiles.map((profile) => ({
const: profile,
title: signedIn(profile) ? profile : `${profile} — not signed in`,
})),
description: `Which ${name} account, by its profile. If the owner did not name one, ask which — never guess.${states}`,
},
profile:
profiles.length === 0
? // An empty enum matches nothing, and a directory that checks the schema refuses the tool.
{
type: "string",
description: `Which ${name} account, by its profile. None is set up on this computer yet.`,
}
: {
type: "string",
enum: [...profiles],
oneOf: profiles.map((one) => ({ const: one, title: signedIn(one) ? one : `${one} — not signed in` })),
description: `Which ${name} account, by its profile. If the owner did not name one, ask which — never guess.${states}`,
},
...schema.properties,
},
required: ["profile", ...(schema.required ?? []).filter((field) => field !== "profile")],
Expand Down Expand Up @@ -242,13 +250,19 @@ export const allProfilesServer = (options: AllProfilesOptions) => {
const profiles = [...options.profiles()].sort()
const server = new McpServer(
{ name: options.command, version: options.version },
{ capabilities: { tools: {} }, instructions: options.instructions },
{
capabilities: { tools: {} },
// Read per session, as the profiles are: clients cut the instructions short, so this line comes first.
instructions:
profiles.length === 0 && options.setup
? `${options.setup.charAt(0).toUpperCase()}${options.setup.slice(1)}.\n${options.instructions}`
: options.instructions,
},
)
server.server.setRequestHandler("tools/list", async () => ({
tools:
profiles.length === 0
? []
: (await tools(profiles)).map((one) => withProfile(one, profiles, options.name, signedIn)),
tools: (profiles.length === 0 ? ((await options.catalogue?.()) ?? []) : await tools(profiles)).map((one) =>
withProfile(one, profiles, options.name, signedIn),
),
}))
const callers = {
env: options.env ?? process.env,
Expand All @@ -258,6 +272,8 @@ export const allProfilesServer = (options: AllProfilesOptions) => {
server.server.setRequestHandler("tools/call", async (request, ctx) => {
const { name, arguments: given = {}, _meta } = request.params
const { profile, ...rest } = given as Record<string, unknown>
if (profiles.length === 0)
return refusal("configuration_error", options.setup ?? `no ${options.command} profile on this computer yet.`)
if (typeof profile !== "string" || !profiles.includes(profile))
return refusal(
"validation_error",
Expand Down Expand Up @@ -287,6 +303,44 @@ export const allProfilesServer = (options: AllProfilesOptions) => {
return { build, close: () => children.close() }
}

/**
* What a server answers to `tools/list`, asked in this process: the list for a session with no profile, which has no
* profile's server to ask.
*/
export const listedTools = async (server: McpServer): Promise<Tool[]> => {
const [own, other] = InMemoryTransport.createLinkedPair()
const answers = new Map<number, (result?: Record<string, unknown>, error?: { message?: string }) => void>()
other.onmessage = (message) => {
const { id, result, error } = message as {
id?: number
result?: Record<string, unknown>
error?: { message?: string }
}
if (typeof id === "number") answers.get(id)?.(result, error)
}
const ask = (id: number, method: string, params: Record<string, unknown>) =>
new Promise<Record<string, unknown>>((resolve, reject) => {
answers.set(id, (result, error) =>
error || !result ? reject(new Error(error?.message ?? `${method} had no answer`)) : resolve(result),
)
void other.send({ jsonrpc: "2.0", id, method, params })
})
await server.connect(own)
try {
await ask(1, "initialize", {
protocolVersion: PROTOCOL,
capabilities: {},
clientInfo: { name: PROXY_CLIENT_NAME, version: "1" },
})
await other.send({ jsonrpc: "2.0", method: "notifications/initialized" })
const answer = await ask(2, "tools/list", {})
if (!Array.isArray(answer.tools)) throw new Error("the catalogue server listed no tools")
return answer.tools as Tool[]
} finally {
await server.close()
}
}

/** Over stdin and stdout until the client closes stdin or the process is told to stop; then the children close. */
export const serveAllProfilesOverStdio = async (
options: AllProfilesOptions,
Expand Down
Loading