types.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 = McpActiveClient | McpErrorClient;
  68. interface McpActiveClient {
  69. client: Client;
  70. tools: ListToolsResponse;
  71. errorMsg: null;
  72. }
  73. interface McpErrorClient {
  74. client: null;
  75. tools: null;
  76. errorMsg: string;
  77. }
  78. // 服务器状态类型
  79. export type ServerStatus = "undefined" | "active" | "paused" | "error";
  80. export interface ServerStatusResponse {
  81. status: ServerStatus;
  82. errorMsg: string | null;
  83. }
  84. // MCP 服务器配置相关类型
  85. export interface ServerConfig {
  86. command: string;
  87. args: string[];
  88. env?: Record<string, string>;
  89. status?: "active" | "paused" | "error";
  90. }
  91. export interface McpConfigData {
  92. // MCP Server 的配置
  93. mcpServers: Record<string, ServerConfig>;
  94. }
  95. export const DEFAULT_MCP_CONFIG: McpConfigData = {
  96. mcpServers: {},
  97. };
  98. export interface ArgsMapping {
  99. // 参数映射的类型
  100. type: "spread" | "single" | "env";
  101. // 参数映射的位置
  102. position?: number;
  103. // 参数映射的 key
  104. key?: string;
  105. }
  106. export interface PresetServer {
  107. // MCP Server 的唯一标识,作为最终配置文件 Json 的 key
  108. id: string;
  109. // MCP Server 的显示名称
  110. name: string;
  111. // MCP Server 的描述
  112. description: string;
  113. // MCP Server 的仓库地址
  114. repo: string;
  115. // MCP Server 的标签
  116. tags: string[];
  117. // MCP Server 的命令
  118. command: string;
  119. // MCP Server 的参数
  120. baseArgs: string[];
  121. // MCP Server 是否需要配置
  122. configurable: boolean;
  123. // MCP Server 的配置 schema
  124. configSchema?: {
  125. properties: Record<
  126. string,
  127. {
  128. type: string;
  129. description?: string;
  130. required?: boolean;
  131. minItems?: number;
  132. }
  133. >;
  134. };
  135. // MCP Server 的参数映射
  136. argsMapping?: Record<string, ArgsMapping>;
  137. }