@gensx/mcp
TypeScript icon, indicating that this package has built-in type declarations

0.1.13 • Public • Published

@gensx/mcp

Model Context Protocol Support for GenSX

Installation

npm install @gensx/mcp

Usage

The @gensx/mcp package provides integration with the Model Context Protocol (MCP) for GenSX workflows. It allows you to use MCP tools in your GenSX applications.

Creating an MCP Server Context

import { createMCPServerContext } from "@gensx/mcp";
import * as gensx from "@gensx/core";

// Create an MCP server context for Sequential Thinking
const {
  Provider: SequentialThinkingProvider,
  useContext: useSequentialThinkingContext,
} = createMCPServerContext({
  clientName: "GSX-MCP-Tools-Client",
  clientVersion: "1.0.0",
  serverCommand: "npx",
  serverArgs: ["-y", "@modelcontextprotocol/server-sequential-thinking"],
});

Using MCP Tools with LLM Providers

You can use MCP tools with any LLM provider in GenSX. Here's an example with OpenAI:

import { createMCPServerContext, MCPTool } from "@gensx/mcp";
import { GSXChatCompletion, OpenAIProvider } from "@gensx/openai";
import * as gensx from "@gensx/core";

// Helper function to map MCP tools to GSX tools
const mapToGsxTools = (tools: MCPTool[]) => {
  return tools.map((tool) =>
    GSXTool.create({
      name: tool.name,
      description: tool.description || "",
      schema: tool.schema,
      run: async (params) => await tool.run(params),
    }),
  );
};

// Create a component that uses MCP tools
const RespondWithTools = gensx.Component(({ userInput }) => {
  const { tools } = useSequentialThinkingContext();
  const gsxTools = mapToGsxTools(tools);

  return (
    <GSXChatCompletion
      model="gpt-4o"
      messages={[
        {
          role: "system",
          content:
            "You are a helpful assistant that can use tools to answer questions.",
        },
        {
          role: "user",
          content: userInput,
        },
      ]}
      tools={gsxTools}
    />
  );
});

// Create a workflow that combines MCP tools with an LLM provider
const MCPToolsWorkflow = gensx.Workflow(
  "MCPToolsWorkflow",
  gensx.Component(({ userInput }) => {
    return (
      <OpenAIProvider apiKey={process.env.OPENAI_API_KEY}>
        <SequentialThinkingProvider>
          <RespondWithTools userInput={userInput}>
            {(result) => result.choices[0].message.content}
          </RespondWithTools>
        </SequentialThinkingProvider>
      </OpenAIProvider>
    );
  }),
);

// Run the workflow
const result = await MCPToolsWorkflow.run({
  userInput:
    "Can you calculate how much flooring I need for a 25x25 room with a 3.5x3 ft area missing in one corner?",
});

API Reference

createMCPServerContext(serverDefinition)

Creates a context provider and hook for accessing MCP tools.

Parameters:

  • serverDefinition: An object with the following properties:
    • clientName: Name of the MCP client
    • clientVersion: Version of the MCP client
    • serverCommand: Command to start the MCP server
    • serverArgs: Arguments for the server command

Returns:

  • Provider: A GenSX component that provides the MCP context
  • useContext: A hook to access the MCP context

MCPTool

A class representing an MCP tool.

Properties:

  • name: The name of the tool
  • description: The description of the tool
  • schema: The Zod schema for the tool's input

Methods:

  • run(params): Runs the tool with the given parameters

Package Sidebar

Install

npm i @gensx/mcp

Weekly Downloads

34

Version

0.1.13

License

Apache-2.0

Unpacked Size

102 kB

Total Files

32

Last publish

Collaborators

  • jmoseley
  • evanboyle