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
15 changes: 9 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ Each level catches what the one before cannot.
It lists the bundled snapshot (`src/tools.json`), and every tool call answers that
`BYTEBASE_URL` is not set.
3. **A real instance**: `scripts/check-live.mjs` starts the server as a client would, lists tools,
and runs two read-only calls. Signed out, the first list is empty and the browser sign-in opens
as soon as the server starts; the first call waits for it.
and runs two read-only calls. Signed out, the first list is the bundled snapshot and the browser
sign-in opens as soon as the server starts; the first call waits for it.

```bash
BYTEBASE_URL=http://localhost:8080 BYTEBASE_MCP_HOME="$(mktemp -d)" node scripts/check-live.mjs
Expand Down Expand Up @@ -140,13 +140,16 @@ The automated tests do not cover these:
must honor `tools/list_changed`).
- [ ] A confirmation form (for example approving an issue) left open for more than a minute can
still be answered, and the answer reaches Bytebase.
- [ ] Clients not yet exercised: Cursor, Claude Desktop, Windsurf. Windows has never been run.
- [ ] In Claude Desktop, from a signed-out start through the config file: a Code-tab session lists
Bytebase's tools, the sign-in opens one browser tab, and Chat and the Code tab share it.
- [ ] Clients not yet exercised: Cursor, Windsurf. Windows has never been run.

## Keeping the snapshot current

`src/tools.json` is the tool list MCP directories see before an instance is configured; a
configured server never reads it. Before a release, regenerate it from an instance on the latest
Bytebase release, never a development build, so directories list no tool users cannot get yet:
`src/tools.json` is the tool list MCP directories see before an instance is configured, and the
list a configured server offers until its session opens. Before a release, regenerate it from an
instance on the latest Bytebase release, never a development build, so no one is shown a tool
users cannot get yet:

```bash
npm run build
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Every command takes `--url <bytebase-url>` in place of `BYTEBASE_URL`.

## Tools

Once you sign in, the tools are the ones your instance serves, so an older or newer Bytebase shows its own. The table below is a snapshot, the list MCP directories see before any instance is configured:
Once you sign in, the tools are the ones your instance serves, so an older or newer Bytebase shows its own. The table below is the bundled snapshot. MCP directories show it when no instance is configured, and your client shows it until you sign in:

<!-- tools:start -->
Generated from Bytebase 3.25.0.
Expand All @@ -106,10 +106,10 @@ Generated from Bytebase 3.25.0.

## How it works

1. With `BYTEBASE_URL` set, the server answers `initialize` at once. If you have signed in before, it opens a session first and passes on your instance's own instructions; otherwise it starts the browser sign-in, and announces your instance's tools with `tools/list_changed` once you have signed in.
1. With `BYTEBASE_URL` set, the server answers `initialize` at once. If you have signed in before, it opens a session first and passes on your instance's own instructions and tools. Otherwise it lists the bundled snapshot and starts the browser sign-in. A tool call made before then waits for the sign-in, or starts one. Once you have signed in, it announces your instance's own tools with `tools/list_changed`.
2. It connects to `<BYTEBASE_URL>/mcp` over Streamable HTTP. With no valid token it runs the OAuth authorization-code flow with PKCE: discovery through `/.well-known/oauth-protected-resource/mcp`, dynamic client registration, browser sign-in, loopback redirect.
3. Tool lists, tool calls, results and confirmation forms pass through unchanged, paging included.
4. Without `BYTEBASE_URL`, the server lists the bundled snapshot so MCP directories can show Bytebase's tools, and every tool call says to set `BYTEBASE_URL`. A configured server never uses the snapshot.
4. Without `BYTEBASE_URL`, the server lists the bundled snapshot so MCP directories can show Bytebase's tools, and every tool call says to set `BYTEBASE_URL`. A configured server lists the snapshot only until your session opens.

The design, with every state the server can be in, is in [docs/DESIGN.md](docs/DESIGN.md).

Expand Down
131 changes: 89 additions & 42 deletions docs/DESIGN.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bytebase/bytebase-mcp",
"version": "0.1.0",
"version": "0.1.1",
"mcpName": "io.github.bytebase/bytebase-mcp",
"description": "Official MCP server package for Bytebase. Reads the tool list from your Bytebase instance and forwards every call to its OAuth-protected /mcp endpoint.",
"repository": {
Expand Down
4 changes: 2 additions & 2 deletions server.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
"source": "github"
},
"websiteUrl": "https://docs.bytebase.com/integrations/mcp",
"version": "0.1.0",
"version": "0.1.1",
"packages": [
{
"registryType": "npm",
"identifier": "@bytebase/bytebase-mcp",
"version": "0.1.0",
"version": "0.1.1",
"transport": {
"type": "stdio"
},
Expand Down
4 changes: 4 additions & 0 deletions src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ export interface ClientIdentity {
clientName: string;
}

/** Claude Desktop's Chat process and its Code-tab and Cowork process share one sign-in (D6). */
const CLAUDE_DESKTOP: ClientIdentity = { key: "claude-ai", clientName: "Claude via Bytebase MCP" };

export function clientIdentity(info: Implementation | undefined): ClientIdentity {
const name = info?.name?.trim() || "unknown-client";
if (name === "claude-ai" || name.startsWith("local-agent-mode-")) return CLAUDE_DESKTOP;
const label = info?.title?.trim() || name;
return {
key: name.replace(/[^A-Za-z0-9._-]/g, "_").slice(0, 100),
Expand Down
45 changes: 33 additions & 12 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,32 @@ function declaresForm(capabilities: ClientCapabilities | undefined): boolean {
return Boolean(elicitation.form) || !elicitation.url;
}

function snapshotNote(): string {
return `Until then, the tools listed are bytebase-mcp's snapshot of Bytebase ${loadBundledTools().bytebaseVersion}, which may differ from the instance's own.`;
}

/** S3 and S4: the package's paragraph for a configured proxy with no session. */
function signedOutInstructions(instance: string, probe: InstanceProbe, capabilities: ClientCapabilities | undefined): string {
const text =
probe.kind === "bytebase"
? `Sign in to Bytebase at ${instance} in the browser window this server opens. Its tools appear once you have signed in.`
? `Sign in to Bytebase at ${instance} in the browser window this server opens; the first tool call waits for the sign-in. ${snapshotNote()}`
: probe.kind === "unreachable"
? `The Bytebase instance at ${instance} could not be reached (${probe.detail}). Its tools appear once it can be reached and you have signed in.`
: `${instance} does not answer as a Bytebase instance (${probe.detail}). Check that BYTEBASE_URL is the instance's external URL (Settings > General > External URL).`;
? `The Bytebase instance at ${instance} could not be reached (${probe.detail}). Its own tools are listed once it can be reached and you have signed in. ${snapshotNote()}`
: `${instance} does not answer as a Bytebase instance (${probe.detail}). Check that BYTEBASE_URL is the instance's external URL (Settings > General > External URL). The tools listed are bytebase-mcp's snapshot and cannot run until it is.`;
return declaresForm(capabilities) ? text : `${text}\n\n${FORM_SENTENCE}`;
}

/** Rule 4: the instance refused a tool name the client got from the snapshot (D2). */
function unknownToolResult(name: string): CallToolResult {
const text = `Bytebase MCP: this Bytebase instance has no tool named "${name}". The tool list came from bytebase-mcp's snapshot of Bytebase ${loadBundledTools().bytebaseVersion}, listed before you signed in. Reconnect this MCP server to get the instance's own tools.`;
return { isError: true, content: [{ type: "text", text }] };
}

/**
* Builds the stdio MCP server. Without a URL it lists the bundled snapshot,
* for directory listings, and runs nothing; with one it lists only what the
* instance serves once a session is open (docs/DESIGN.md, D2).
* for directory listings, and runs nothing. With one it lists the snapshot
* until a session has answered tools/list, then only what the instance serves
* (docs/DESIGN.md, D2).
*/
export function createServer(config: Config): { server: Server; upstream: () => Upstream | undefined } {
const server = new Server(
Expand All @@ -100,6 +111,8 @@ export function createServer(config: Config): { server: Server; upstream: () =>
// client on this SDK would keep the first withdrawn form up (D5).
(server as unknown as { _requestMessageId: number })._requestMessageId = 1;
let initialized = false;
// Whether the client may still hold the snapshot as its tool list (D2).
let listedFromSession = false;

// Created at initialize: a sign-in belongs to the client application (D6).
let upstream: Upstream | undefined;
Expand Down Expand Up @@ -146,7 +159,7 @@ export function createServer(config: Config): { server: Server; upstream: () =>

server.oninitialized = () => {
initialized = true;
// A configured proxy without a session has no tools to offer, so it signs in now (D5).
// A configured proxy without a session can only list the snapshot, so it signs in now (D5).
if (!config.url || upstream?.session) return;
void signInWhenReachable(config.url);
};
Expand Down Expand Up @@ -195,21 +208,23 @@ export function createServer(config: Config): { server: Server; upstream: () =>

server.setRequestHandler(ListToolsRequestSchema, async (request) => {
if (!config.url) return { tools: loadBundledTools().tools };
// Without a session there is nothing to list yet (D2), and tools/list never
// waits on a sign-in: the tools are announced when the session opens. With
// a stored sign-in and no sign-in under way, a session dropped by an outage
// Until a session has answered, the snapshot, never an empty list, and
// tools/list never waits on a sign-in: the instance's tools are announced
// when the session opens (D2). After that, a session dropped by an outage
// is reopened here (D5), within a bound.
const target = upstreamFor(config.url);
if (!target.session && (target.signingIn || !target.provider.hasStoredSignIn())) return { tools: [] };
if (!target.session && !listedFromSession) return { tools: loadBundledTools().tools };
// Only a wait spent reopening a dropped session ends in an announcement; a
// slow answer on an open session changes nothing the client holds.
const reopening = !target.session;
try {
return await withinBound((signal) => target.listTools(request.params?.cursor, signal), LIST_BOUND_MS,
const page = await withinBound((signal) => target.listTools(request.params?.cursor, signal), LIST_BOUND_MS,
// The reconnect carries on; when it opens, the client is told to list again.
() => {
if (reopening) announceWhenOpen(target);
}, `still connecting to ${config.url.toString().replace(/\/$/, "")}`);
listedFromSession = true;
return page;
} catch (err) {
if (err instanceof McpError) throw err;
// tools/list has no error result, so the reason goes in a JSON-RPC error (rule 4).
Expand All @@ -232,7 +247,13 @@ export function createServer(config: Config): { server: Server; upstream: () =>
}
return result;
} catch (err) {
if (err instanceof McpError) throw err;
if (err instanceof McpError) {
// The Go SDK's refusal of a tool it does not serve (rule 4).
if (!listedFromSession && err.code === ErrorCode.InvalidParams && /unknown tool/i.test(err.message)) {
return unknownToolResult(request.params.name);
}
throw err;
}
return { isError: true, content: [{ type: "text", text: `Bytebase MCP: ${describe(err)}` }] };
}
});
Expand Down
3 changes: 2 additions & 1 deletion src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import type { Tool } from "@modelcontextprotocol/sdk/types.js";
* The tool definitions shipped with this package, dumped from a Bytebase
* release's built-in MCP server by `npm run sync-tools`. They let a directory
* (Glama, the MCP Registry, awesome lists) start the server and read its tool
* list without a Bytebase instance; a connected instance's live list wins.
* list without a Bytebase instance, and they are a configured server's list
* until its session answers (docs/DESIGN.md, D2).
*/
export interface BundledTools {
/** Bytebase version the definitions were read from. */
Expand Down
16 changes: 16 additions & 0 deletions test/config.test.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import { loadConfig, mcpEndpoint, parseBytebaseUrl } from "../dist/config.js";
import { clientIdentity } from "../dist/auth.js";

test("D6: clientIdentity maps Claude Desktop's client names to one sign-in", () => {
const desktop = { key: "claude-ai", clientName: "Claude via Bytebase MCP" };
assert.deepEqual(clientIdentity({ name: "claude-ai", version: "1" }), desktop, "Chat");
assert.deepEqual(clientIdentity({ name: "local-agent-mode-Bytebase", version: "1" }), desktop, "Code tab and Cowork");
assert.deepEqual(clientIdentity({ name: "local-agent-mode-bytebase-dev", version: "1" }), desktop, "any server name");
assert.deepEqual(
clientIdentity({ name: "Bytebase-era-probe", version: "1" }),
{ key: "Bytebase-era-probe", clientName: "Bytebase-era-probe via Bytebase MCP" },
"the startup probe keeps its own",
);
assert.deepEqual(clientIdentity({ name: "cursor-vscode", title: "Cursor", version: "1" }), { key: "cursor-vscode", clientName: "Cursor via Bytebase MCP" });
assert.deepEqual(clientIdentity({ name: "my agent/1", version: "1" }), { key: "my_agent_1", clientName: "my agent/1 via Bytebase MCP" });
assert.equal(clientIdentity(undefined).key, "unknown-client");
});

test("parseBytebaseUrl normalizes trailing slash and /mcp", () => {
for (const raw of ["https://bb.example.com", "https://bb.example.com/", "https://bb.example.com/mcp", "https://bb.example.com/mcp/", "bb.example.com"]) {
Expand Down
6 changes: 6 additions & 0 deletions test/fake-upstream.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ export async function startFakeBytebase({ resourceMetadata = true, pageSize = 0,
if (message.method === "tools/call") {
toolCalls.push(message.params);
if (state.callDelayMs > 0) await new Promise((resolve) => setTimeout(resolve, state.callDelayMs));
// Bytebase's Go SDK refuses a tool it does not serve with this JSON-RPC error (server.go).
const served = [...FAKE_TOOLS.tools.map((tool) => tool.name), ...(reauthorizeTool ? ["reauthorize"] : []), ...(streamed ? ["confirm_change"] : [])];
if (!served.includes(message.params?.name)) {
json(res, 200, { jsonrpc: "2.0", id: message.id, error: { code: -32602, message: `unknown tool "${message.params?.name}"` } });
return;
}
}
if (message.method === "tools/list") {
toolLists.push(message.params);
Expand Down
Loading
Loading