client.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { Client } from "@modelcontextprotocol/sdk/client/index.js";
  2. import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
  3. import { MCPClientLogger } from "./logger";
  4. import { McpRequestMessage } from "./types";
  5. import { z } from "zod";
  6. export interface ServerConfig {
  7. command: string;
  8. args?: string[];
  9. env?: Record<string, string>;
  10. }
  11. const logger = new MCPClientLogger();
  12. export async function createClient(
  13. serverConfig: ServerConfig,
  14. name: string,
  15. ): Promise<Client> {
  16. logger.info(`Creating client for server ${name}`);
  17. const transport = new StdioClientTransport({
  18. command: serverConfig.command,
  19. args: serverConfig.args,
  20. env: serverConfig.env,
  21. });
  22. const client = new Client(
  23. {
  24. name: `nextchat-mcp-client-${name}`,
  25. version: "1.0.0",
  26. },
  27. {
  28. capabilities: {
  29. // roots: {
  30. // listChanged: true,
  31. // },
  32. },
  33. },
  34. );
  35. await client.connect(transport);
  36. return client;
  37. }
  38. export interface Primitive {
  39. type: "resource" | "tool" | "prompt";
  40. value: any;
  41. }
  42. /** List all resources, tools, and prompts */
  43. export async function listPrimitives(client: Client): Promise<Primitive[]> {
  44. const capabilities = client.getServerCapabilities();
  45. const primitives: Primitive[] = [];
  46. const promises = [];
  47. if (capabilities?.resources) {
  48. promises.push(
  49. client.listResources().then(({ resources }) => {
  50. resources.forEach((item) =>
  51. primitives.push({ type: "resource", value: item }),
  52. );
  53. }),
  54. );
  55. }
  56. if (capabilities?.tools) {
  57. promises.push(
  58. client.listTools().then(({ tools }) => {
  59. tools.forEach((item) => primitives.push({ type: "tool", value: item }));
  60. }),
  61. );
  62. }
  63. if (capabilities?.prompts) {
  64. promises.push(
  65. client.listPrompts().then(({ prompts }) => {
  66. prompts.forEach((item) =>
  67. primitives.push({ type: "prompt", value: item }),
  68. );
  69. }),
  70. );
  71. }
  72. await Promise.all(promises);
  73. return primitives;
  74. }
  75. /** Execute a request */
  76. export async function executeRequest(
  77. client: Client,
  78. request: McpRequestMessage,
  79. ) {
  80. return client.request(request, z.any());
  81. }