数学计算协议服务器

一个通过wolframscript执行Mathematica代码并验证数学推导的模型上下文协议服务器,适用于复杂数学计算和推导验证场景。

  • by texra-ai
  • JavaScript
  • 本地部署
  • 24 星标
  • 更新于
在 GitHub 查看 texra-ai/mcp-server-mathematica

使用说明

Mathematica MCP服务器

此存储库包含一个模型上下文协议(MCP)服务器,允许MCP客户端(如Cursor)通过以下方式执行Mathematica代码 wolframscript 并验证数学推导。

概述

此服务器充当桥梁,使支持MCP的应用程序能够利用本地Mathematica安装的功能来执行以下任务:

  • 执行复杂的数学计算。
  • 验证人类或人工智能模型提供的数学推导步骤。
  • 生成表达式的LaTeX或Mathematica字符串表示。

先决条件

  • 数学软件 必须安装在您的系统上。
  • 这 wolframscript 命令行实用程序必须在系统的PATH中可用。您可以通过运行来测试这一点 wolframscript -help 在你的终端。
  • Node.js (推荐:v16或更高版本,从中推断 tsconfig.json 目标 ES2022).

安装

  1. 克隆存储库:
    git clone <repository-url>
    cd <repository-directory>
    
  2. 安装依赖项:
    npm install
    
  3. 构建服务器:
    npm run build
    
    此命令从以下位置编译TypeScript源代码 src/ 在JavaScript中 build/ 目录,并使主脚本可执行。

运行服务器

要启动MCP服务器,请在终端中运行以下命令:

node build/index.js

服务器将启动并通过标准输入/输出(stdio)监听来自MCP客户端的连接。当您打算使用服务器时,请保持此终端窗口打开。

为了实现更稳健的部署,可以考虑使用流程管理器,如 pm2 在后台运行服务器并管理重启。

与MCP客户端(如Cursor、Cline、Claude Desktop)集成

MCP客户端通常会发现正在运行的MCP服务器并与之通信。具体的配置步骤可能因客户端应用程序而异。

一般步骤:

  1. 启动Mathematica MCP服务器: 确保服务器在终端中运行:
    node build/index.js
    
  2. 配置您的MCP客户端: 将服务器添加到客户端的配置中。这通常涉及编辑JSON设置文件。请参阅下面的客户特定说明。
  3. 重新启动MCP客户端: 启动服务器或更改配置后,重新启动客户端应用程序以确保它检测到Mathematica服务器。

客户端特定配置:

  • 克莱恩: 根据 临床MCP服务器开发协议,通常在设置文件中配置服务器(通常 settings.json 在Cline配置目录中)。您可以添加这样的条目:

    {
      "mcpServers": {
        "mathematica-server": {
          "command": "node",
          "args": ["/full/path/to/mcp-server-mathematica/build/index.js"], // Replace with the actual absolute path
          "disabled": false,
          "autoApprove": [] // Optional: Add tool names to auto-approve
        }
        // ... other servers ...
      }
    }
    

    替换 /full/path/to/mcp-server-mathematica/build/index.js 通往建筑的绝对路径 index.js 您系统上的文件。

  • 光标: 游标可能需要编辑特定的设置文件,可能如下 ~/Library/Application Support/Cursor/User/globalStorage/saoudrizwan.claude-dev/settings/cline_mcp_settings.json (尽管这条路可能会改变)。该结构将类似于上述Cline示例。

  • 其他客户(例如Claude Desktop): 请查阅您特定MCP客户端的文档。查找“MCP服务器”、“工具配置”或“外部工具”部分。配置通常涉及指定命令(node),服务器脚本的路径(build/index.js),以及可能的环境变量(如果需要)。

可用工具

服务器向MCP客户端公开以下工具:

1. execute_mathematica

执行任意Mathematica代码并返回结果。

输入架构:

{
  type: "object",
  properties: {
    code: {
      type: "string",
      description: "Mathematica code to execute"
    },
    format: {
      type: "string",
      description: "Output format (text, latex, or mathematica)",
      enum: ["text", "latex", "mathematica"],
      default: "text"
    }
  },
  required: ["code"]
}

示例用法(客户端请求):

  • 自然语言: “使用Mathematica计算x^2从0到1的积分,格式为LaTeX”
  • 直接工具调用:
    {
      "tool_name": "execute_mathematica",
      "arguments": {
        "code": "Integrate[x^2, {x, 0, 1}]",
        "format": "latex"
      }
    }
    

2. verify_derivation

验证一系列数学表达式,以检查每个步骤是否在逻辑上遵循前一个步骤 Simplify[prev == current].

输入架构:

{
  type: "object",
  properties: {
    steps: {
      type: "array",
      description: "Array of mathematical expressions (as strings) representing steps in a derivation. Requires at least two steps.",
      items: {
        type: "string"
      }
    },
    format: {
      type: "string",
      description: "Output format for the verification report (text, latex, or mathematica)",
      enum: ["text", "latex", "mathematica"],
      default: "text"
    }
  },
  required: ["steps"]
}

示例用法(客户端请求):

  • 自然语言: “验证此推导:['x^2-y^2','(x-y)(x+y)']”
  • 直接工具调用:
    {
      "tool_name": "verify_derivation",
      "arguments": {
        "steps": [
          "x^2 - y^2",
          "(x-y)*(x+y)"
        ],
        "format": "text"
      }
    }
    

故障排除

  • 找不到服务器/没有响应:
    • 确保服务器在终端中运行(node build/index.js).
    • 检查是否 wolframscript 已安装并可在PATH中访问(wolframscript -help).
    • 重新启动MCP客户端应用程序。
    • 检查客户端的MCP配置。
  • 工具错误:
    • 检查服务器的终端输出(stderr)中的日志和错误消息 wolframscript.
    • 验证Mathematica的语法 code 或 steps 提供。
    • 确保 steps 数组用于 verify_derivation 至少有两个元素。
  • Mathematica问题: 确保您的Mathematica安装已获得许可并正常工作。

项目结构

  • src/:服务器的TypeScript源代码。
  • build/:编译的JavaScript输出(由生成) npm run build).
  • package.json:项目元数据和依赖关系。
  • tsconfig.json:TypeScript编译器配置。