UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

152 lines • 5.33 kB
import { ServerConnection } from "./connection.js"; import { ConnectError } from "./errors.js"; import { registerMcpClientAlias } from "./request-origin.js"; const requestMethods = new Set([ "request", "listTools", "listResources", "listResourceTemplates", "listPrompts", "readResource", "getPrompt", "subscribeResource", "unsubscribeResource", "complete", "ping", "setLoggingLevel", ]); /** Session view: only the host can renew/dispose the physical connection. */ export class SharedMcpLease extends ServerConnection { #unsubscribeTools; #stateSubscriptions = new Set(); #released = false; #requestLifetime = new AbortController(); get requestLifetimeSignal() { return this.#requestLifetime.signal; } #localGeneration = 0; #client; #physicalClient; #refreshCatalog = false; constructor(shared, options, owner, key) { super(options); this.shared = shared; this.options = options; this.owner = owner; this.key = key; this.#unsubscribeTools = shared.connection.onToolsChanged((event) => this.markToolsChanged(event.cause)); } get state() { return this.#released ? "disabled" : this.shared.connection.state; } get generation() { return this.shared.connection.generation + this.#localGeneration; } get lastError() { return this.shared.connection.lastError; } get client() { this.#assertAttached(); const physical = this.shared.connection.client; if (this.#physicalClient !== physical) { this.#physicalClient = physical; const callTool = (...args) => { this.#assertAttached(); return this.shared.callTool(this, () => physical.callTool(...args)); }; this.#client = new Proxy(physical, { get: (target, property) => { if (property === "callTool") return callTool; const value = Reflect.get(target, property, target); if (typeof value === "function" && requestMethods.has(property)) { return (...args) => { this.#assertAttached(); return this.shared.runRequest(() => Promise.resolve(Reflect.apply(value, target, args))); }; } return typeof value === "function" ? value.bind(target) : value; }, }); registerMcpClientAlias(this.#client, physical); } if (this.#client === undefined) throw new ConnectError("MCP client unavailable", { phase: "client" }); return this.#client; } async connect() { this.#assertAttached(); await this.shared.connect(); return this.client; } async renew() { await this.bumpGeneration(); return this.connect(); } async bumpGeneration() { this.#assertAttached(); this.#requestLifetime.abort(); this.#requestLifetime = new AbortController(); this.#localGeneration++; this.#refreshCatalog = true; // Re-list only this owner's catalog; never interrupt another owner's call. this.markToolsChanged(); } async catalog() { const refresh = this.#refreshCatalog; this.#refreshCatalog = false; return this.shared.catalog(refresh); } getRootPid() { return this.#released ? null : this.shared.connection.getRootPid(); } refreshCapturedDiagnostics() { this.shared.connection.refreshCapturedDiagnostics(); } // A released lease no longer speaks for the shared connection: a late failure seen through it must not degrade // the connection the surviving owners still use (senpi#4013). markDegraded(error) { if (!this.#released) this.shared.connection.markDegraded(error); } markSuspended(error) { if (!this.#released) this.shared.connection.markSuspended(error); } markNeedsAuth(error) { if (!this.#released) this.shared.connection.markNeedsAuth(error); } markNeedsClientRegistration(error) { if (!this.#released) this.shared.connection.markNeedsClientRegistration(error); } onStateChange(listener) { const off = this.shared.connection.onStateChange((event) => listener({ ...event, generation: this.generation })); this.#stateSubscriptions.add(off); return () => { this.#stateSubscriptions.delete(off); off(); }; } async disable() { await this.dispose(); } async dispose() { if (this.#released) return; this.#released = true; this.#requestLifetime.abort(); this.#unsubscribeTools(); for (const off of this.#stateSubscriptions) off(); this.#stateSubscriptions.clear(); this.shared.release(this); } #assertAttached() { if (this.#released) { throw new ConnectError("MCP connection owner detached", { phase: "client", serverName: this.serverName }); } } } //# sourceMappingURL=shared-lease.js.map