types.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // ref: https://spec.modelcontextprotocol.io/specification/basic/messages/
  2. import { z } from "zod";
  3. import { Client } from "@modelcontextprotocol/sdk/client/index.js";
  4. export interface McpRequestMessage {
  5. jsonrpc?: "2.0";
  6. id?: string | number;
  7. method: "tools/call" | string;
  8. params?: {
  9. [key: string]: unknown;
  10. };
  11. }
  12. export const McpRequestMessageSchema: z.ZodType<McpRequestMessage> = z.object({
  13. jsonrpc: z.literal("2.0").optional(),
  14. id: z.union([z.string(), z.number()]).optional(),
  15. method: z.string(),
  16. params: z.record(z.unknown()).optional(),
  17. });
  18. export interface McpResponseMessage {
  19. jsonrpc?: "2.0";
  20. id?: string | number;
  21. result?: {
  22. [key: string]: unknown;
  23. };
  24. error?: {
  25. code: number;
  26. message: string;
  27. data?: unknown;
  28. };
  29. }
  30. export const McpResponseMessageSchema: z.ZodType<McpResponseMessage> = z.object(
  31. {
  32. jsonrpc: z.literal("2.0").optional(),
  33. id: z.union([z.string(), z.number()]).optional(),
  34. result: z.record(z.unknown()).optional(),
  35. error: z
  36. .object({
  37. code: z.number(),
  38. message: z.string(),
  39. data: z.unknown().optional(),
  40. })
  41. .optional(),
  42. },
  43. );
  44. export interface McpNotifications {
  45. jsonrpc?: "2.0";
  46. method: string;
  47. params?: {
  48. [key: string]: unknown;
  49. };
  50. }
  51. export const McpNotificationsSchema: z.ZodType<McpNotifications> = z.object({
  52. jsonrpc: z.literal("2.0").optional(),
  53. method: z.string(),
  54. params: z.record(z.unknown()).optional(),
  55. });
  56. ////////////
  57. // Next Chat
  58. ////////////
  59. export interface ListToolsResponse {
  60. tools: {
  61. name?: string;
  62. description?: string;
  63. inputSchema?: object;
  64. [key: string]: any;
  65. };
  66. }
  67. export type McpClientData =
  68. | McpActiveClient
  69. | McpErrorClient
  70. | McpInitializingClient;
  71. interface McpInitializingClient {
  72. client: null;
  73. tools: null;
  74. errorMsg: null;
  75. }
  76. interface McpActiveClient {
  77. client: Client;
  78. tools: ListToolsResponse;
  79. errorMsg: null;
  80. }
  81. interface McpErrorClient {
  82. client: null;
  83. tools: null;
  84. errorMsg: string;
  85. }
  86. // 服务器状态类型
  87. export type ServerStatus =
  88. | "undefined"
  89. | "active"
  90. | "paused"
  91. | "error"
  92. | "initializing";
  93. export interface ServerStatusResponse {
  94. status: ServerStatus;
  95. errorMsg: string | null;
  96. }
  97. // MCP 服务器配置相关类型
  98. export interface ServerConfig {
  99. command: string;
  100. args: string[];
  101. env?: Record<string, string>;
  102. status?: "active" | "paused" | "error";
  103. }
  104. export interface McpConfigData {
  105. // MCP Server 的配置
  106. mcpServers: Record<string, ServerConfig>;
  107. }
  108. export const DEFAULT_MCP_CONFIG: McpConfigData = {
  109. mcpServers: {},
  110. };
  111. export interface ArgsMapping {
  112. // 参数映射的类型
  113. type: "spread" | "single" | "env";
  114. // 参数映射的位置
  115. position?: number;
  116. // 参数映射的 key
  117. key?: string;
  118. }
  119. export interface PresetServer {
  120. // MCP Server 的唯一标识,作为最终配置文件 Json 的 key
  121. id: string;
  122. // MCP Server 的显示名称
  123. name: string;
  124. // MCP Server 的描述
  125. description: string;
  126. // MCP Server 的仓库地址
  127. repo: string;
  128. // MCP Server 的标签
  129. tags: string[];
  130. // MCP Server 的命令
  131. command: string;
  132. // MCP Server 的参数
  133. baseArgs: string[];
  134. // MCP Server 是否需要配置
  135. configurable: boolean;
  136. // MCP Server 的配置 schema
  137. configSchema?: {
  138. properties: Record<
  139. string,
  140. {
  141. type: string;
  142. description?: string;
  143. required?: boolean;
  144. minItems?: number;
  145. }
  146. >;
  147. };
  148. // MCP Server 的参数映射
  149. argsMapping?: Record<string, ArgsMapping>;
  150. }