UNPKG

@smartsamurai/krapi-sdk

Version:

KRAPI TypeScript SDK - Easy-to-use client SDK for connecting to self-hosted KRAPI servers (like Appwrite SDK)

85 lines (75 loc) 2.71 kB
/** * MCP Adapter * * Unifies MCPHttpClient and MCPService behind a common interface. */ import { MCPHttpClient } from "../../http-clients/mcp-http-client"; import { MCPService, ChatMessage, ChatResponse, Model, ModelCapabilities } from "../../mcp-service"; import { createAdapterInitError } from "./error-handler"; type Mode = "client" | "server"; export class MCPAdapter { private mode: Mode; private httpClient: MCPHttpClient | undefined; private service: MCPService | undefined; constructor(mode: Mode, httpClient?: MCPHttpClient, service?: MCPService) { this.mode = mode; this.httpClient = httpClient; this.service = service; } async chat(projectId: string, messages: ChatMessage[]): Promise<ChatResponse> { if (this.mode === "client") { if (!this.httpClient) { throw createAdapterInitError("HTTP client", this.mode); } const response = await this.httpClient.chat(projectId, messages); return (response.data as unknown as ChatResponse) || ({} as ChatResponse); } else { if (!this.service) { throw createAdapterInitError("MCP service", this.mode); } return await this.service.chat(projectId, messages); } } async adminChat(messages: ChatMessage[]): Promise<ChatResponse> { if (this.mode === "client") { if (!this.httpClient) { throw createAdapterInitError("HTTP client", this.mode); } const response = await this.httpClient.adminChat(messages); return (response.data as unknown as ChatResponse) || ({} as ChatResponse); } else { if (!this.service) { throw createAdapterInitError("MCP service", this.mode); } return await this.service.adminChat(messages); } } async getModelCapabilities(): Promise<ModelCapabilities> { if (this.mode === "client") { if (!this.httpClient) { throw createAdapterInitError("HTTP client", this.mode); } const response = await this.httpClient.getModelCapabilities(); return response.data || ({} as ModelCapabilities); } else { if (!this.service) { throw createAdapterInitError("MCP service", this.mode); } return await this.service.getModelCapabilities(); } } async listModels(): Promise<Model[]> { if (this.mode === "client") { if (!this.httpClient) { throw createAdapterInitError("HTTP client", this.mode); } const response = await this.httpClient.listModels(); return (response.data as unknown as Model[]) || []; } else { if (!this.service) { throw createAdapterInitError("MCP service", this.mode); } return await this.service.listModels(); } } }