opencode-mcp
Version:
MCP server for OpenCode coding workflows, durable async jobs, project resources, and explicit user input
154 lines (153 loc) • 5.96 kB
TypeScript
/**
* Smart response formatting helpers.
*
* Instead of dumping raw JSON to the LLM, these helpers extract the
* meaningful content from OpenCode API responses so the LLM can reason
* about them efficiently.
*/
import { z } from "zod";
/** Read-only tool: does not modify state. */
export declare const readOnly: {
readonly readOnlyHint: true;
readonly destructiveHint: false;
};
/** Destructive tool: permanently deletes data or shuts down services. */
export declare const destructive: {
readonly readOnlyHint: false;
readonly destructiveHint: true;
};
/**
* Shared Zod parameter for project directory targeting.
* When provided, sent as the x-opencode-directory header so the
* OpenCode server scopes the request to that project.
*/
export declare const directoryParam: z.ZodOptional<z.ZodString>;
/** OpenCode SDK OutputFormat, accepted by synchronous and async prompts. */
export declare const outputFormatParam: z.ZodOptional<z.ZodDiscriminatedUnion<[z.ZodObject<{
type: z.ZodLiteral<"text">;
}, z.core.$strip>, z.ZodObject<{
type: z.ZodLiteral<"json_schema">;
schema: z.ZodRecord<z.ZodString, z.ZodUnknown>;
retryCount: z.ZodOptional<z.ZodNumber>;
}, z.core.$strip>], "type">>;
/**
* Set the global default provider and model.
* Called once from index.ts during startup.
*/
export declare function setModelDefaults(providerID?: string, modelID?: string): void;
/**
* Apply model defaults: use explicit params if both are provided,
* otherwise fall back to env-var defaults if both are set,
* otherwise return undefined (let the server decide).
*
* Returns `{ providerID, modelID }` or `undefined`.
*/
export declare function applyModelDefaults(providerID?: string, modelID?: string): {
providerID: string;
modelID: string;
} | undefined;
/**
* Validate a server-side absolute path without interpreting it on this host.
* POSIX, Windows drive and UNC paths are supported on every client platform.
* Preserve separators, symlinks and dot segments for the server to resolve.
* Local-only filesystem tools perform their own validation separately.
*/
export declare function normalizeDirectory(directory?: string): string | undefined;
/**
* Extract a human-readable summary from a message response.
* Pulls text content from parts, summarizes tool calls, etc.
* Accepts any shape — casts internally for safety.
*/
export declare function formatMessageResponse(response: unknown): string;
/**
* Format a list of messages, extracting text content from each.
*
* When the assistant message has no text content (common with some providers
* that only emit tool calls), we show a concise summary of tool actions
* instead of blank output. Cost/token metadata from step-finish parts is
* appended when available.
*/
export declare function formatMessageList(messages: unknown[]): string;
/**
* Format a diff response into a readable summary.
*/
export declare function formatDiffResponse(diffs: unknown[]): string;
/**
* Format session objects for LLM-friendly display.
*/
export declare function formatSessionList(sessions: unknown[]): string;
/**
* Serialize JSON, returning a valid JSON preview envelope for large values.
* The preview is deliberately a string: it must never be mistaken for complete data.
*/
export declare function safeStringify(value: unknown, maxLength?: number): string;
/**
* Analyze an AI message response for signs of failure:
* - Completely empty (null/undefined)
* - Has parts but no text content (provider returned nothing)
* - Contains error indicators in parts
*
* Returns a diagnostic object with `isEmpty`, `hasError`, and `warning` text.
*/
export declare function analyzeMessageResponse(response: unknown): {
isEmpty: boolean;
hasError: boolean;
warning: string | null;
};
/**
* Redact values that look like API keys, tokens, or secrets.
* Replaces the value with the first 4 characters + "***REDACTED***".
* Works recursively on objects and arrays.
*
* Three layers of detection:
* 1. Key-name based: key names matching sensitive patterns (KEY, TOKEN, SECRET, etc.)
* 2. Value-based: string values matching known API key prefixes or long hex/base64 tokens
* 3. URL-based: query parameters in URL strings that contain secrets
*/
export declare function redactSecrets(value: unknown): unknown;
/**
* Determine whether a provider object from the OpenCode API is truly configured
* (i.e. has usable credentials), as opposed to being a built-in default.
*
* Detection layers:
* - source "env" / "config" / "api" → always configured
* - source "custom" with a non-empty apiKey → configured
* - source "custom" for "anthropic" with extra option keys (OAuth sets headers) → configured
* - Everything else → not configured
*/
export declare function isProviderConfigured(p: Record<string, unknown>): boolean;
/**
* Resolve a session status value from the OpenCode API.
*
* The API may return status as a plain string ("idle", "running") or as an
* object like `{ state: "running", ... }`. This helper normalises both forms
* into a human-readable string.
*/
export declare function resolveSessionStatus(raw: unknown): string;
/**
* Standard tool response builder.
*/
export declare function toolResult(text: string, isError?: boolean, structuredContent?: Record<string, unknown>): {
structuredContent?: Record<string, unknown> | undefined;
isError?: boolean | undefined;
content: {
type: "text";
text: string;
}[];
};
export declare function toolError(e: unknown): {
structuredContent?: Record<string, unknown> | undefined;
isError?: boolean | undefined;
content: {
type: "text";
text: string;
}[];
};
export declare function toolJson(value: unknown): {
structuredContent?: Record<string, unknown> | undefined;
isError?: boolean | undefined;
content: {
type: "text";
text: string;
}[];
};