xsAI v0.4 "AIAIAI" is now available! Read Announcement
xsAI0.4.4
Tools

Model Context Protocol

Model Context Protocol adapter for xsAI.

This code is untested, feel free to test it yourself and provide feedback.

npm i @modelcontextprotocol/sdk @xsai/tool
import type { Client } from "@modelcontextprotocol/sdk/client/index.js";
import type { Tool } from "@xsai/shared-chat";

export const getTools = (mcpServers: Record<string, Client>): Promise<Tool[]> =>
  Promise.all(
    Object.entries(mcpServers)
      .map(([serverName, client]) =>
        client
          .listTools()
          .then(({ tools }) =>
            tools.map(({ description, inputSchema, name }) => ({
              execute: (args: unknown) =>
                client.callTool({
                  arguments: args as Record<string, unknown>,
                  name,
                }).then((res) => JSON.stringify(res)),
              function: {
                description,
                name: name === serverName ? name : `${serverName}_${name}`,
                parameters: inputSchema,
                strict: true,
              },
              type: "function",
            } satisfies Tool))
          )
      ),
  ).then((tools) => tools.flat());

Examples

import { Client } from '@modelcontextprotocol/sdk/client/index.js'
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js'

import { getTools } from './utils/get-tools'

const transport = new StdioClientTransport({ command: 'node', args: ['server.js'] })
const client = new Client({ name: 'example-client', version: '1.0.0' })

await client.connect(transport)

const tools = await getTools({ example: client })

On this page