LangChain MCP工具限制器

一个用于LangChain MCP客户端的工具和参数限制包装器,提供对AI代理的工具访问增强安全和控制。

  • by MimoryInc
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 MimoryInc/mimory-mcp-focus-langchainjs

使用说明

mimory mcp焦点langchainjs

LangChain MCP(模型上下文协议)客户端的焦点强制包装器,允许您限制AI代理可以使用哪些工具和参数,从而增强对MCP工具访问的安全性和控制。

特性

  • 工具限制:限制您的AI代理可以使用哪些MCP工具
  • 参数验证:使用简单模式或范围限制参数值
  • 多服务器支持:跨多个MCP服务器管理焦点配置
  • LangChain集成:与LangChain的MCP适配器无缝集成
  • TypeScript支持:完整的TypeScript定义,以获得更好的开发体验
  • 灵活的配置:支持简单和复合聚焦模式

安装

npm install mimory-mcp-focus-langchainjs

对等依赖关系

此包需要以下对等依赖关系:

npm install @langchain/mcp-adapters @mimory/mim-mcp-focus-js

快速开始

import { FocusMultiServerMCPClient } from 'mimory-mcp-focus-langchainjs';

// Define MCP server connections
const connections = {
  everything: {
    transport: "stdio",
    command: "npx",
    args: ["-y", "@modelcontextprotocol/server-everything"]
  }
};

// Configure focus restrictions
const focusConfigs = {
  everything: {
    focusTools: ["echo"], // Only allow echo tool
    focusParams: {
      message: ["hello", "world", "test"] // Restrict message values
    },
    focusType: "simple",
    strict: false
  }
};

// Create focus-enabled client
const client = new FocusMultiServerMCPClient(connections, focusConfigs);

// Get restricted tools
const tools = await client.getTools();
console.log("Available tools:", tools);

焦点配置模式

简单专注

简单的焦点允许您限制所有工具的工具和参数:

const focusConfig = {
  focusTools: ["tool1", "tool2"],
  focusParams: {
    param1: ["value1", "value2"],
    param2: ["range:1-100"]
  },
  focusType: "simple",
  strict: false
};

复合焦点

复合焦点允许每个工具有不同的限制:

const focusConfig = {
  focus: {
    tool1: {
      param1: ["value1", "value2"],
      param2: ["range:1-10"]
    },
    tool2: {
      param3: ["value3", "value4"]
    }
  },
  focusType: "composite",
  strict: true
};

api参考

FocusMultiServerMCPClient

将LangChain的MultiServerMCPClient与焦点功能封装在一起的主类。

构造函数

constructor(
  connections?: Record<string, Connection>,
  focusConfigs?: Record<string, FocusConfig>
)

方法

  • getTools(serverName?: string):从应用了焦点的指定服务器获取工具
  • getClient(serverName: string):为特定服务器获取一个专注的MCP客户端
  • close():关闭所有连接并清理
  • getServerFocusConfig(serverName: string):获取服务器的焦点配置
  • setServerFocusConfig(serverName: string, focusConfig: FocusConfig):设置服务器的焦点配置

工厂功能

createFocusClientSimple

function createFocusClientSimple(
  connections: Record<string, Connection>,
  focusTools?: Record<string, FocusTools>,
  focusParams?: Record<string, FocusParams>,
  strict: boolean = false
): FocusMultiServerMCPClient

createFocusComposite

function createFocusClientComposite(
  connections: Record<string, Connection>,
  focus?: Record<string, FocusComposite>,
  strict: boolean = false
): FocusMultiServerMCPClient

例子

基本工具限制

import { FocusMultiServerMCPClient } from 'mimory-mcp-focus-langchainjs';

const connections = {
  everything: {
    transport: "stdio",
    command: "npx",
    args: ["-y", "@modelcontextprotocol/server-everything"]
  }
};

const focusConfigs = {
  everything: {
    focusTools: ["echo"],
    focusParams: {
      message: ["hello", "world", "test"]
    },
    focusType: "simple",
    strict: false
  }
};

const client = new FocusMultiServerMCPClient(connections, focusConfigs);
const tools = await client.getTools();

// Use tools with LangChain agents
const tool = tools[0];
const result = await tool.invoke({ message: "hello" });

多服务器设置

const connections = {
  everything: {
    transport: "stdio",
    command: "npx",
    args: ["-y", "@modelcontextprotocol/server-everything"]
  },
  time: {
    transport: "stdio",
    command: "python",
    args: ["-m", "mcp_server_time"]
  }
};

const focusConfigs = {
  everything: {
    focus: {
      echo: {
        message: ["hello", "world", "composite", "focus"]
      },
      add: {
        a: ["range:1-5"],
        b: ["range:1-5"]
      }
    },
    focusType: "composite",
    strict: false
  },
  time: {
    focusTools: ["get_current_time"],
    focusParams: {
      timezone: ["America/New_York", "Europe/London", "Asia/Tokyo"]
    },
    focusType: "simple",
    strict: false
  }
};

const client = new FocusMultiServerMCPClient(connections, focusConfigs);

与LangChain代理一起使用

import { ChatOpenAI } from "@langchain/openai";
import { createReactAgent } from "@langchain/core/agents";
import { FocusMultiServerMCPClient } from 'mimory-mcp-focus-langchainjs';

// Create focus-enabled client
const client = new FocusMultiServerMCPClient(connections, focusConfigs);
const tools = await client.getTools();

// Create LangChain agent with restricted tools
const model = new ChatOpenAI({ model: "gpt-4" });
const agent = await createReactAgent({
  llm: model,
  tools,
  messagesPlaceholder: "You are a helpful assistant with access to restricted tools."
});

// Use the agent
const result = await agent.invoke({
  input: "Echo the message 'hello'"
});

支持的MCP服务器

所有MCP服务器

实现各种MCP功能的测试服务器:

设置:

npm install -g @modelcontextprotocol/server-everything@latest

可用工具:

  • echo:回显输入消息
  • add:将两个数字相加

时间MCP服务器

提供时间和时区转换功能:

设置:

pip install mcp-server-time

可用工具:

  • get_current_time:获取特定时区的当前时间
  • convert_time:在时区之间转换时间

错误处理

该库为焦点相关问题提供了全面的错误处理:

try {
  const result = await tool.invoke({ message: "forbidden" });
} catch (error) {
  if (error.name === 'FocusError') {
    console.log('Focus restriction violated:', error.message);
  } else {
    console.log('Other error:', error.message);
  }
}

配置选项

FocusConfig接口

interface FocusConfig {
  focusTools?: FocusTools;           // Allowed tools
  focusParams?: FocusParams;         // Parameter restrictions
  focus?: FocusComposite;            // Composite focus configuration
  focusType?: 'simple' | 'composite'; // Focus type
  strict?: boolean;                  // Strict mode enforcement
}

参数值模式

  • 精确值: ["value1", "value2"]
  • 范围: ["range:1-100"] 用于数值范围
  • 通配符: ["*"] 允许所有值

贡献

欢迎投稿!请随时提交拉取请求。

许可证

此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。

相关项目

支持

如需支持,请在 GitHub 或联系我们oss@mimory.io.

作者

特拉维斯·麦昆 - oss@mimory.io


该库是模拟生态系统的一部分,为人工智能应用程序提供对MCP工具的安全和受控访问。