jsonv-ts
Version:
JSON Schema builder and validator for TypeScript with static type inference, Hono middleware for OpenAPI generation and validation, and MCP server/client implementation. Lightweight, dependency-free, and built on Web Standards.
98 lines (97 loc) • 4.29 kB
TypeScript
import { RpcMessage, type TRpcId, type TRpcRawRequest, type TRpcResponse } from "./rpc";
import * as s from "jsonv-ts";
import { Tool, type ToolConfig, type ToolHandler } from "./tool";
import { Resource, type ResourceConfig, type ResourceHandler, type TResourceUri } from "./resource";
declare const serverInfoSchema: s.ObjectSchema<{
readonly name: s.StringSchema<s.IStringOptions> & s.IStringOptions;
readonly version: s.StringSchema<s.IStringOptions> & s.IStringOptions;
}, s.Merge<s.IObjectOptions & {
additionalProperties: false;
}>>;
export type McpServerInfo = s.Static<typeof serverInfoSchema>;
export declare const logLevels: {
emergency: string;
alert: string;
critical: string;
error: string;
warning: string;
notice: string;
info: string;
debug: string;
};
export type LogLevel = keyof typeof logLevels;
export declare const logLevelNames: LogLevel[];
export declare const protocolVersion = "2025-06-18";
export declare class McpServer<ServerContext extends object = {}, Tools extends Tool<any, any, any | never>[] = Tool<any, any, any | never>[], Resources extends Resource<any, any, any, any>[] = Resource<any, any, any, any>[]> {
readonly serverInfo: s.Static<typeof serverInfoSchema>;
readonly context: ServerContext;
tools: Tools;
resources: Resources;
protected readonly messages: RpcMessage<string, s.Schema>[];
readonly version = "2025-06-18";
protected currentId: TRpcId | undefined;
_id: string;
protected logLevel: LogLevel;
readonly history: Map<TRpcId, {
request: TRpcRawRequest;
response?: TRpcResponse;
}>;
protected onNotificationListener?: (message: TRpcRawRequest) => void;
constructor(serverInfo?: s.Static<typeof serverInfoSchema>, context?: ServerContext, tools?: Tools, resources?: Resources);
onNotification(handler: (message: TRpcRawRequest) => void): this;
clone(): McpServer<ServerContext, Tools, Resources>;
setLogLevel(level: LogLevel): void;
addTool<T extends Tool<any, any, any | never>>(tool: T): this;
tool<Name extends string, Config extends ToolConfig | undefined = undefined>(name: Name, config: Config, handler: ToolHandler<Config, ServerContext>): this;
addResource<R extends Resource<any, any, any, any>>(resource: R): this;
resource<Name extends string, Uri extends TResourceUri, Handler extends ResourceHandler<Uri, ServerContext>, Config extends ResourceConfig | undefined = undefined>(name: Name, uri: Uri, handler: Handler, config?: Config): this;
get console(): { [K in keyof typeof logLevels]: (...args: any[]) => void; };
handle(payload: TRpcRawRequest, raw?: unknown): Promise<TRpcResponse>;
toJSON(): {
serverInfo: {
name: string;
version: string;
};
tools: {
name: any;
title: string | undefined;
description: string | undefined;
inputSchema: s.JSONSchemaDefinition;
outputSchema: s.JSONSchemaDefinition | undefined;
annotations: {
title?: string | undefined;
readOnlyHint?: boolean | undefined;
destructiveHint?: boolean | undefined;
idempotentHint?: boolean | undefined;
openWorldHint?: boolean | undefined;
} | undefined;
_meta: {
[key: string]: unknown;
} | undefined;
}[];
resources: {
[x: string]: string | number | {
[key: string]: unknown;
} | undefined;
name: any;
title: string | undefined;
description: string | undefined;
mimeType: string | undefined;
size: number | undefined;
_meta: {
[key: string]: unknown;
} | undefined;
}[];
};
}
export interface McpServerOptions {
tools?: Tool<any, any, any | never>[];
resources?: Resource<any, any, any | never>[];
context?: object;
serverInfo?: McpServerInfo;
logLevel?: LogLevel;
}
export declare function mcpServer<Opts extends McpServerOptions>(opts: Opts): McpServer<object, Tool<any, any, any, any>[], Resource<any, any, any, {} | {
[x: string]: string;
}>[]>;
export {};