模块化工具带
A. 模型上下文协议 (MCP)公开的服务器 Wasm组件 作为工具。
这是一个早期的非生产原型。
构建
先决条件:电流 防锈工具链
克隆 可组合mcp 项目,如果你还没有。
然后从内部 composable-mcp 目录:
cargo install --path .这将构建二进制文件 release 配置文件并添加 将其添加到您的货箱目录中,该目录应位于您的PATH中。
运行简单组件
提供一条或多条路径 .wasm 文件作为命令行参数:
toolbelt hello.wasm calculator.wasm或者,您可以为已发布的Wasm组件指定OCI URI,例如:
toolbelt oci://ghcr.io/modulewise/demo/hello:0.2.0 \
oci://ghcr.io/modulewise/demo/calculator:0.2.0\[!提示\] 如果要在本地构建Wasm组件,请克隆 模块化/演示 项目并遵循中的构建说明 组件/README.md
运行具有依赖关系的组件
默认情况下,组件以最低权限功能模式运行。 如果您的组件需要主机运行时的功能,您可以 在a中指定这些功能 .toml 文件:
[capability.http]
type = "wasi:http"然后定义导入一个或多个功能的工具组件:
[component.flights]
uri = "file:///path/to/flight-search.wasm"
imports = ["http"]将定义文件传递给服务器,而不是直接传递 .wasm 文件夹:
toolbelt flights.tomlWasm Components还可以导入可能有自己依赖关系的其他组件:
components.toml
[component.incrementor]
uri = "../demos/components/lib/incrementor.wasm"
imports = ["keyvalue"]
[component.incrementor.config]
bucket = "increments"
[component.keyvalue]
uri = "../demos/components/lib/valkey-client.wasm"
imports = ["wasip2"]责任可以在多个文件中分开:
capabilities.toml
[capability.wasip2]
type = "wasi:p2"现在可以将这些文件传递到服务器:
toolbelt components.toml capabilities.toml这允许主机功能和客户组件的各种组合。 它还促进了责任驱动的关注点分离 支持基础设施和以领域为中心的工具。
配置MCP服务器
当否 [server] 随着 type = "mcp" 已配置,工具带启动默认值 MCP服务器已打开 127.0.0.1:3001 自动发现顶级组件(那些 未由其他组件进口)。
对于显式控制,添加 [server] 部分与 type = "mcp" 致你的 定义文件。
使用选择器发现要作为工具公开的组件
使用 component-selector 按元数据匹配组件:
[server.mcp]
type = "mcp"
port = 3001
component-selector = "!dependents"这将所有顶级组件作为工具公开。其他选择器表达式 例如,支持 labels.domain = "shopping" 或 name = "greeter".
明确定义工具
使用 [server.mcp.tool.*] 将特定组件功能映射到的条目 命名工具:
[server.mcp]
type = "mcp"
port = 3001
[server.mcp.tool.greeter]
component = "greeter"
function = "greet"
description = "Greet someone by name"
[server.mcp.tool.adder]
component = "calculator"
function = "operations.add"工具名称来自TOML键(greeter 和 adder 上文)。 如果指定了函数,则该函数应通过接口进行限定 通过接口导出,或者如果在WIT世界级导出,则直接导出。 这 description 字段是可选的,但在提供 向呼叫代理发送指令。
两者结合
选择器和显式工具可以一起使用。明确的工具定义 优先处理名称冲突:
[server.mcp]
type = "mcp"
port = 3001
component-selector = "!dependents"
[server.mcp.tool.greeter]
component = "greeter"
function = " greet"
description = "Custom description for the greet tool"原产地验证
工具带验证 Origin 根据MCP规范,请求中是否存在标头。 绑定到环回地址时,默认情况下允许使用localhost源。 否则,除非明确配置,否则所有来源都将被拒绝:
[server.mcp]
type = "mcp"
port = 3001
host = "0.0.0.0"
allowed-origins = ["app.example.com", "localhost"]OpenTetry跟踪
添加 otlp-endpoint 通过OTLP导出跨度:
[server.mcp]
type = "mcp"
port = 3001
otlp-endpoint = "http://localhost:4317"
otlp-protocol = "grpc" # or "http/protobuf"; defaults to "grpc"跨度为 tools/list 和 tools/call 属性如下 gen_ai MCP语义约定跟踪上下文通过从客户端传播到服务器 _meta 在请求参数和从服务器到Wasm组件调用中 traceparent 在传播背景下。
看 示例/酒店 以Jaeger为例。
使用MCP检查员进行测试
- 如上所述运行服务器。
- 启动 MCP检查员.
- 确保
Streamable HTTP已选择传输类型。
- 确保指定的URL为
http://127.0.0.1:3001/mcp(如果不使用默认值,请更换主机或端口)。
- 点击
Connect然后List Tools.
- 选择一个工具,提供参数值,然后单击
Run Tool.
许可证
版权所有(c)2026 Modulewise Inc和Modulewise可组合MCP贡献者。
Apache许可证v2.0:请参阅 许可证 了解详情。
