types.ts 3.1 KB

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