opencode-mcp
Version:
MCP server for OpenCode coding workflows, durable async jobs, project resources, and explicit user input
277 lines • 13.1 kB
JavaScript
import { createOpencodeClient } from "@opencode-ai/sdk";
import { normalizeDirectory } from "./helpers.js";
import { ensureServer, isServerRunning } from "./server-manager.js";
import { AuthProvider } from "./auth.js";
import { createRequestContext, abortableSleep, throwIfAborted, withAbort } from "./async.js";
export class OpenCodeError extends Error {
status;
method;
path;
body;
constructor(message, status, method, path, body) {
super(message);
this.status = status;
this.method = method;
this.path = path;
this.body = body;
this.name = "OpenCodeError";
}
get isTransient() {
return (this.status === 429 ||
this.status === 502 ||
this.status === 503 ||
this.status === 504);
}
get isNotFound() {
return this.status === 404;
}
get isAuth() {
return this.status === 401 || this.status === 403;
}
}
/** The server may have accepted a write whose response was lost. */
export class OpenCodeSubmissionError extends Error {
method;
path;
submissionOutcome = "unknown";
constructor(method, path, cause) {
super(`${method} ${path}: submission outcome unknown; the server may have accepted this operation. Check its session before retrying. ${cause instanceof Error ? cause.message : String(cause)}`, { cause });
this.method = method;
this.path = path;
this.name = "OpenCodeSubmissionError";
}
}
const MAX_RETRIES = 2;
const BASE_DELAY_MS = 500;
const MAX_RECONNECT_ATTEMPTS = 3;
function isConnectionError(err) {
const msg = err.message?.toLowerCase() || "";
return (msg.includes("econnrefused") ||
msg.includes("enotfound") ||
msg.includes("ehostunreach") ||
msg.includes("fetch failed") ||
msg.includes("network error") ||
msg.includes("socket hang up"));
}
export class OpenCodeClient {
api;
baseUrl;
autoServe;
reconnectAttempts = 0;
username;
password;
auth;
constructor(options) {
this.baseUrl = options.baseUrl.replace(/\/$/, "");
this.autoServe = options.autoServe ?? false;
this.username = options.username;
this.password = options.password;
this.auth = new AuthProvider(options, this.baseUrl);
this.api = this.buildSdkClient();
}
/**
* Rebuild the SDK client against the current `baseUrl` + auth state. Used
* by the constructor and by the reconnect path when `ensureServer()`
* surfaces a different URL than the one we were aiming at.
*/
buildSdkClient() {
return createOpencodeClient({ baseUrl: this.baseUrl, fetch: (request) => this.auth.fetch(request) });
}
getBaseUrl() {
return this.baseUrl;
}
/** Current `Authorization` header value; signs in to OpenChamber when configured. */
getAuthorization() {
return this.auth.getHeader();
}
async request(method, path, opts = {}) {
// Validate before submission. A malformed local argument cannot be an
// ambiguous remote mutation and must never trigger retries.
const normalized = normalizeDirectory(opts.directory);
const headers = normalized ? { "x-opencode-directory": normalized } : {};
const context = createRequestContext(opts);
const readOnly = method === "GET";
let lastError;
let submitted = false;
try {
for (let attempt = 0; attempt <= (readOnly ? MAX_RETRIES : 0); attempt++) {
throwIfAborted(context.signal);
if (attempt > 0)
await abortableSleep(BASE_DELAY_MS * Math.pow(2, attempt - 1), context.signal);
try {
const apiClient = this.api._client;
submitted = true;
const res = await withAbort(apiClient[method.toLowerCase()]({
url: path, query: opts.query,
...(method !== "GET" && method !== "DELETE" ? { body: opts.body } : {}),
headers, signal: context.signal,
}), context.signal);
// The SDK may return an error object after fetch was aborted.
throwIfAborted(context.signal);
if (res.error || (res.response && !res.response.ok && res.response.status >= 400)) {
const status = res.response?.status || 500;
const bodyStr = typeof res.error === "string" ? res.error : JSON.stringify(res.error ?? {});
throw new OpenCodeError(`${method} ${path} failed (${status}): ${bodyStr}`, status, method, path, bodyStr);
}
this.reconnectAttempts = 0;
return res.data;
}
catch (e) {
// Writes may already have been accepted. No generic HTTP status,
// disconnect or timeout proves it is safe to send them a second time.
if (!readOnly) {
if (e instanceof OpenCodeError)
throw e;
throw new OpenCodeSubmissionError(method, path, e);
}
throwIfAborted(context.signal);
lastError = e instanceof Error ? e : new Error(String(e));
if (e instanceof OpenCodeError && !e.isTransient)
throw e;
if (!(e instanceof OpenCodeError) && !isConnectionError(lastError))
throw e;
}
}
if (this.autoServe && this.reconnectAttempts < MAX_RECONNECT_ATTEMPTS && lastError && isConnectionError(lastError)) {
this.reconnectAttempts++;
console.error(`Connection failed (attempt ${this.reconnectAttempts}/${MAX_RECONNECT_ATTEMPTS}), attempting server reconnection...`);
try {
const authorization = await withAbort(this.auth.getHeader(), context.signal);
const status = await withAbort(isServerRunning(this.baseUrl, this.username, this.password, authorization), context.signal);
if (!status.healthy) {
throwIfAborted(context.signal);
const ensured = await withAbort(ensureServer({
baseUrl: this.baseUrl, autoServe: true, username: this.username, password: this.password, authorization,
}), context.signal);
if (ensured.url) {
const nextUrl = ensured.url.replace(/\/$/, "");
if (nextUrl !== this.baseUrl) {
this.baseUrl = nextUrl;
this.api = this.buildSdkClient();
}
}
}
return await this.request(method, path, { ...opts, signal: context.signal, deadline: context.deadline });
}
catch (e) {
throwIfAborted(context.signal);
console.error(`Server reconnection failed: ${e instanceof Error ? e.message : String(e)}`);
}
}
throw lastError ?? new Error(`${method} ${path} failed after retries`);
}
catch (error) {
// Preflight cancellation has no submission ambiguity.
if (!readOnly && submitted && !(error instanceof OpenCodeError) && !(error instanceof OpenCodeSubmissionError)) {
throw new OpenCodeSubmissionError(method, path, error);
}
throw error;
}
finally {
context.dispose();
}
}
async get(path, query, directory, options) {
return this.request("GET", path, { ...options, query, directory });
}
async post(path, body, opts) {
return this.request("POST", path, { ...opts, body });
}
async patch(path, body, directory, options) {
return this.request("PATCH", path, { ...options, body, directory });
}
async put(path, body, directory, options) {
return this.request("PUT", path, { ...options, body, directory });
}
async delete(path, query, directory, options) {
return this.request("DELETE", path, { ...options, query, directory });
}
async *subscribeSSE(path, opts = {}) {
// `new URL(path, this.baseUrl)` resolves an absolute `path` against the
// origin, discarding any base path (e.g. OpenChamber's `/api` proxy
// prefix). Join on the string form of baseUrl instead, which already has
// its trailing slash stripped in the constructor.
const url = `${this.baseUrl}${path}`;
const headers = { Accept: "text/event-stream", "Cache-Control": "no-cache" };
const normalized = normalizeDirectory(opts.directory);
if (normalized)
headers["x-opencode-directory"] = normalized;
const context = createRequestContext(opts, 30_000);
let reader;
const cancelReader = () => { void reader?.cancel().catch(() => { }); };
try {
throwIfAborted(context.signal);
const res = await withAbort(this.auth.fetch(new Request(url, { method: "GET", headers, signal: context.signal })), context.signal);
if (!res.ok) {
const text = await withAbort(res.text(), context.signal);
throw new OpenCodeError(`SSE ${path} failed (${res.status}): ${text}`, res.status, "GET", path, text);
}
if (!res.body)
throw new Error("No response body for SSE stream");
reader = res.body.getReader();
context.signal.addEventListener("abort", cancelReader, { once: true });
const decoder = new TextDecoder();
let buffer = "";
let event = "";
let data = [];
while (true) {
throwIfAborted(context.signal);
const { done, value } = await withAbort(reader.read(), context.signal);
throwIfAborted(context.signal);
buffer += done ? decoder.decode() : decoder.decode(value, { stream: true });
// SSE accepts LF, CRLF and lone CR. Preserve CR across chunk boundaries
// so a split CRLF is consumed as one separator.
let match;
while ((match = /\r\n|\r|\n/.exec(buffer))) {
if (!done && match[0] === "\r" && match.index === buffer.length - 1)
break;
const line = buffer.slice(0, match.index);
buffer = buffer.slice(match.index + match[0].length);
if (data.reduce((sum, item) => sum + item.length, 0) + line.length > 1_000_000) {
throw new Error("SSE event exceeds the 1MB limit");
}
if (line === "") {
if (data.length > 0)
yield { event: event || "message", data: data.join("\n") };
event = "";
data = [];
}
else if (!line.startsWith(":")) {
const separator = line.indexOf(":");
const field = separator < 0 ? line : line.slice(0, separator);
let content = separator < 0 ? "" : line.slice(separator + 1);
if (content.startsWith(" "))
content = content.slice(1);
if (field === "event")
event = content;
if (field === "data")
data.push(content);
}
}
// Ignore an unfinished event at EOF, as required by SSE framing.
if (done)
break;
if (buffer.length + data.reduce((sum, line) => sum + line.length, 0) > 1_000_000) {
throw new Error("SSE event exceeds the 1MB limit");
}
}
}
finally {
context.signal.removeEventListener("abort", cancelReader);
context.abort();
// Cancel the underlying stream on maxEvents/early return as well as
// errors. Releasing the reader lock alone leaves the HTTP stream open.
if (reader) {
// Cancellation itself may be asynchronous in a custom stream; do not
// let cleanup extend the caller's deadline indefinitely.
try {
void reader.cancel().catch(() => { });
}
catch { /* Preserve the stream error. */ }
reader.releaseLock();
}
context.dispose();
}
}
}
//# sourceMappingURL=client.js.map