MCP ExplorerExplorer

Zmcp

@AdjectiveAllisonon 10 months ago
5 MIT
FreeCommunity
AI Systems
A Zig MCP Library

Overview

What is Zmcp

zmcp is a lightweight Zig implementation of the Model Context Protocol (MCP), enabling seamless integration between LLM applications and external tools through compile-time validated interfaces.

Use cases

Use cases for zmcp include creating echo tools, processing input data with structured parameters, and developing applications that require robust error handling and resource management.

How to use

To use zmcp, add it as a dependency using ‘zig fetch’, then include it in your ‘build.zig’ file. Define your tools using structured parameters and implement the tool’s functionality with a handler function.

Key features

Key features of zmcp include type-safe tools with compile-time validation, automatic resource management through allocator injection, error conversion from Zig errors to MCP error responses, and a transport layer with current support for stdio transport.

Where to use

zmcp can be used in various fields where LLM applications require integration with external tools, such as AI development, data processing, and any application needing structured tool interactions.

Content

zmcp - a Zig MCP Library

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.

Features

  • 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)

Installation

  1. Add zmcp as a dependency using zig fetch:
# Latest version
zig fetch --save git+https://github.com/AdjectiveAllison/zmcp.git#main
  1. 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);

Quick Start

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
);

Structured Parameters

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
};

Current Limitations

  • 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

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Tools

No tools

Comments

Recommend MCP Servers

View All MCP Servers