MCP_SERVER.md 5.5 KB

MCP 服务器文档

IJS 内置 MCP (Model Context Protocol) 服务器,可将 Symja 符号计算能力暴露给 AI 客户端(如 Claude Desktop)。

快速开始

Claude Desktop

{
  "mcpServers": {
    "ijs": {
      "url": "http://127.0.0.1:8734/mcp"
    }
  }
}

RikkaHub(Rikka AI)

RikkaHub 使用 Streamable HTTP 传输协议,直接配置即可:

{
  "type": "streamable-http",
  "url": "http://127.0.0.1:8734/mcp",
  "commonOptions": {
    "name": "IJS - Symja 符号计算",
    "enable": true,
    "headers": []
  }
}

支持 SSE 和 Streamable HTTP 双传输协议,自动检测客户端类型。

网络配置

如果客户端和 IJS 不在同一台设备,先通过 adb 转发:

adb forward tcp:8734 tcp:8734

若需局域网访问,在路由器上对 8734 端口做端口转发。顶部状态灯变绿表示服务器运行中。

工具列表

核心计算

工具 参数 示例
evaluate expression "2+2"
differentiate expression, variable "x^3", "x"
integrate expression, variable, lower?, upper? "x^2", "x", "0", "1"
solve equation, variable "x^2-5*x+6==0", "x"
simplify expression "Sin[x]^2+Cos[x]^2"
numeric expression, precision? "Pi", "50"
to_latex expression "x^2+2*x+1"

绘图

工具 参数 示例
plot function, variable, x_min, x_max "Sin[x]", "x", "0", "2*Pi"
parametric_plot x_func, y_func, t_var, t_min, t_max "2*Cos[t]", "2*Sin[t]", "t", "0", "2*Pi"
list_plot points "{{1,1},{2,4},{3,9}}"

绘图工具返回结构化 JSON,包含 Line/Point 点序列和颜色。控制台自动 Canvas 渲染。

插件管理

工具 参数 说明
list_plugins — 列出所有已安装插件
get_plugin plugin_id 获取插件详情及 JS 代码
create_plugin id, name, js_code, icon?, command?, description?, inputs? 创建/更新插件
delete_plugin plugin_id 删除插件
execute_plugin plugin_id, arguments? 执行插件并返回结果

资源

URI 内容
variables://current 当前 Symja 会话中已定义的变量
history://recent 最近通过 MCP 执行的计算历史

响应格式

所有绘图操作返回结构化 JSON:

{
  "type": "plot",
  "elements": [
    {
      "type": "line",
      "color": [0.368, 0.507, 0.710],
      "points": [[0.0, 0.0], [0.1, 0.0998], ...]
    }
  ],
  "options": {"axes": true}
}

工具调用错误时,result.isError 为 true。

插件开发

MCP 可通过 create_plugin 工具远程创建插件。插件 JS 代码中可使用完整的 Symja API:

function calculate(inputs, Symja) {
  Symja.print('计算中...');
  return Symja.eval('Integrate[' + inputs.func + ', ' + inputs.var + ']');
}

可用 API:eval, print, differentiate, integrate, solve, simplify, expand, factor, plot, parametricPlot, listPlot, numeric, toTeX, gcd, lcm, isPrime, fibonacci, binomial, sum, product, limit, 及矩阵运算。

架构

Claude Desktop ──HTTP──▶ 127.0.0.1:8734/mcp
                              │
                    NanoHTTPd (McpServer.java)
                              │
                    JSON-RPC 2.0 dispatch
                              │
              ┌───────────────┼───────────────┐
              ▼               ▼               ▼
        SymjaEngine    PluginManager    Rhino JS
        (数学计算)      (插件管理)     (插件执行)
              │
              ▼
        AppSession (共享会话 + MCP事件队列)
              │
              ▼
        WebView 控制台 (实时同步显示)

插件调试

MCP 提供了插件调试工具,可通过 evaluate 工具触发:

命令 用途
evaluate("__open plugin_id") 在控制台全屏面板打开插件
evaluate("__eval js_code") 在 WebView 中执行 JS,结果通过 MCP 事件返回
evaluate("__tap x y") 模拟画布逻辑坐标点击

调试流程示例:

1. __open power_of_two              → 打开游戏
2. __eval console.log("test")       → 注入调试日志
3. __tap 300 235                    → 模拟点击坐标
4. execute_plugin("api_test")       → 运行 API 测试(含错误堆栈)

通过 resources/read api://plugin 获取完整 API 参考。

IJS API 速览

插件可用的完整 API:

命名空间 主要方法
IJS.Symja eval, print, differentiate, integrate, solve, simplify, expand, factor, numeric, plot, parametricPlot, listPlot, toTeX, gcd, lcm, isPrime, fibonacci, binomial, sum, product, limit, series, apart, together, trigSimplify, matrix.{det, inverse, transpose, eigenvalues, rank}, renderLatex
IJS.canvas create(w?,h?), getContext(), getSize(), width(), height(), clear(), fill(color), size(w,h), loop(cb), stop()
IJS.input xy(event), onTouchStart/Move/End(cb)
IJS.io read/write/list/delete (应用数据目录)
IJS.audio beep(freq,dur,type), playNote(note,oct,dur), playMelody(melody,tempo)
IJS.ui close(), setBackground(color)

内置 JS 库:lodash (_), seedrandom (Math.seedrandom), Matter.js (Matter)

安全

  • 仅监听 127.0.0.1,不对外暴露
  • 无认证(信任本地环境,与主流 MCP 实现一致)
  • 可在设置面板关闭 MCP 服务器