A lightweight Zig implementation of the Model Context Protocol (MCP). ZMCP enables seamless integration between LLM applications and external tools through compile-time validated interfaces.
- Type-Safe Tools: Struct-based parameters with compile-time validation
- Automatic Resource Management: Allocator injection for heap-using tools
- Error Conversion: Zig errors automatically converted to MCP error responses
- Transport Layer: Current support for stdio transport (HTTP+SSE planned)
- MCP Features:
- ✅ Tools API with full schema generation
- ✅ Basic logging support
- ✅ Progress tracking
- ⏳ Resources API (planned)
- ⏳ Prompts API (planned)
- Add zmcp as a dependency using
zig fetch
:
# Latest version
zig fetch --save git+https://github.com/AdjectiveAllison/zmcp.git#main
- Add zmcp as a module in your
build.zig
:
const zmcp_dep = b.dependency("zmcp", .{
.target = target,
.optimize = optimize,
});
const zmcp = zmcp_dep.module("zmcp");
// Add to your executable
exe.root_module.addImport("zmcp", zmcp);
const EchoParams = struct {
allocator: std.mem.Allocator, // Auto-injected by server
message: []const u8,
repeat: u32 = 1 // Default value
};
fn echoFn(params: EchoParams) ![]const u8 {
// ... implementation using params.allocator
}
const echo_tool = zmcp.Tool(
"echo",
"Echo with repetition",
echoFn // Struct-based handler
);
Tools must use a struct parameter containing:
- An optional
allocator
field (auto-injected) - Typed parameters with validation
- Optional fields with default values
- struct keys turn into names for the tool call parameters
Example parameter struct:
const ProcessArgs = struct {
allocator: std.mem.Allocator,
input: []const u8,
iterations: u32 = 10,
verbose: ?bool = null
};
- Only stdio transport supported (HTTP+SSE planned)
- Resources and Prompts APIs not yet implemented
- Limited to JSON-compatible Zig types
- No support for custom transports
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.