diff --git a/src/Config.zig b/src/Config.zig index b0abddaf..20730fa3 100644 --- a/src/Config.zig +++ b/src/Config.zig @@ -24,6 +24,7 @@ const log = @import("log.zig"); const dump = @import("browser/dump.zig"); const WebBotAuthConfig = @import("network/WebBotAuth.zig").Config; +const mcp = @import("mcp.zig"); pub const RunMode = enum { help, @@ -222,6 +223,7 @@ pub const Serve = struct { pub const Mcp = struct { common: Common = .{}, + version: mcp.Version = .default, }; pub const DumpFormat = enum { @@ -453,6 +455,12 @@ pub fn printUsageAndExit(self: *const Config, success: bool) void { \\Starts an MCP (Model Context Protocol) server over stdio \\Example: {s} mcp \\ + \\Options: + \\--version + \\ Override the reported MCP version. + \\ Valid: 2024-11-05, 2025-03-26, 2025-06-18, 2025-11-25. + \\ Defaults to "2024-11-05". + \\ ++ common_options ++ \\ \\version command @@ -640,10 +648,22 @@ fn parseMcpArgs( allocator: Allocator, args: *std.process.ArgIterator, ) !Mcp { - var mcp: Mcp = .{}; + var result: Mcp = .{}; while (args.next()) |opt| { - if (try parseCommonArg(allocator, opt, args, &mcp.common)) { + if (std.mem.eql(u8, "--version", opt)) { + const str = args.next() orelse { + log.fatal(.mcp, "missing argument value", .{ .arg = opt }); + return error.InvalidArgument; + }; + result.version = std.meta.stringToEnum(mcp.Version, str) orelse { + log.fatal(.mcp, "invalid protocol version", .{ .value = str }); + return error.InvalidArgument; + }; + continue; + } + + if (try parseCommonArg(allocator, opt, args, &result.common)) { continue; } @@ -651,7 +671,7 @@ fn parseMcpArgs( return error.UnkownOption; } - return mcp; + return result; } fn parseFetchArgs( diff --git a/src/mcp.zig b/src/mcp.zig index ca92206c..de013bc9 100644 --- a/src/mcp.zig +++ b/src/mcp.zig @@ -1,6 +1,7 @@ const std = @import("std"); pub const protocol = @import("mcp/protocol.zig"); +pub const Version = protocol.Version; pub const router = @import("mcp/router.zig"); pub const Server = @import("mcp/Server.zig"); diff --git a/src/mcp/Server.zig b/src/mcp/Server.zig index ffba7396..a334bc38 100644 --- a/src/mcp/Server.zig +++ b/src/mcp/Server.zig @@ -114,7 +114,7 @@ test "MCP.Server - Integration: synchronous smoke test" { try router.processRequests(server, &in_reader); - try testing.expectJson(.{ .jsonrpc = "2.0", .id = 1 }, out_alloc.writer.buffered()); + try testing.expectJson(.{ .jsonrpc = "2.0", .id = 1, .result = .{ .protocolVersion = "2024-11-05" } }, out_alloc.writer.buffered()); } test "MCP.Server - Integration: ping request returns an empty result" { diff --git a/src/mcp/protocol.zig b/src/mcp/protocol.zig index 1375cd00..02927278 100644 --- a/src/mcp/protocol.zig +++ b/src/mcp/protocol.zig @@ -1,5 +1,14 @@ const std = @import("std"); +pub const Version = enum { + @"2024-11-05", + @"2025-03-26", + @"2025-06-18", + @"2025-11-25", + + pub const default: Version = .@"2024-11-05"; +}; + pub const Request = struct { jsonrpc: []const u8 = "2.0", id: ?std.json.Value = null, diff --git a/src/mcp/router.zig b/src/mcp/router.zig index 02581776..63945947 100644 --- a/src/mcp/router.zig +++ b/src/mcp/router.zig @@ -81,8 +81,12 @@ pub fn handleMessage(server: *Server, arena: std.mem.Allocator, msg: []const u8) fn handleInitialize(server: *Server, req: protocol.Request) !void { const id = req.id orelse return; - const result = protocol.InitializeResult{ - .protocolVersion = "2025-11-25", + const version: protocol.Version = switch (server.app.config.mode) { + .mcp => |opts| opts.version, + else => .default, + }; + const result: protocol.InitializeResult = .{ + .protocolVersion = @tagName(version), .capabilities = .{ .resources = .{}, .tools = .{}, @@ -121,7 +125,7 @@ test "MCP.router - handleMessage - synchronous unit tests" { \\{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test-client","version":"1.0.0"}}} ); try testing.expectJson( - \\{ "jsonrpc": "2.0", "id": 1, "result": { "capabilities": { "tools": {} } } } + \\{ "jsonrpc": "2.0", "id": 1, "result": { "protocolVersion": "2024-11-05", "capabilities": { "tools": {} } } } , out_alloc.writer.buffered()); out_alloc.writer.end = 0;