eggsact 1.2.7

Deterministic MCP and in-process utility tools for coding agents
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
use serde::{Deserialize, Serialize};
use serde_json::Value;

// ═══════════════════════════════════════════════════════════════════════════════
// JSON-RPC error constructors
// ═══════════════════════════════════════════════════════════════════════════════

pub fn json_rpc_error(code: i32, message: impl Into<String>, id: Option<Value>) -> Value {
    serde_json::to_value(JsonRpcError {
        jsonrpc: "2.0".to_string(),
        error: JsonRpcErrorDetail {
            code,
            message: message.into(),
        },
        id,
    })
    .unwrap_or_else(|_| {
        serde_json::json!({
            "jsonrpc": "2.0",
            "error": {"code": -32603, "message": "Internal error: failed to serialize error response"},
            "id": null
        })
    })
}

/// JSON-RPC error with optional structured `data` field for lifecycle errors.
pub fn json_rpc_error_with_data(
    code: i32,
    message: impl Into<String>,
    data: Option<Value>,
    id: Option<Value>,
) -> Value {
    let detail = JsonRpcErrorDetailWithData {
        code,
        message: message.into(),
        data,
    };
    serde_json::to_value(JsonRpcErrorWithData {
        jsonrpc: "2.0".to_string(),
        error: detail,
        id,
    })
    .unwrap_or_else(|_| {
        serde_json::json!({
            "jsonrpc": "2.0",
            "error": {"code": -32603, "message": "Internal error: failed to serialize error response"},
            "id": null
        })
    })
}

pub fn invalid_request(message: impl Into<String>, id: Option<Value>) -> Value {
    json_rpc_error(-32600, message, id)
}

pub fn method_not_found(message: impl Into<String>, id: Option<Value>) -> Value {
    json_rpc_error(-32601, message, id)
}

/// Invalid params (JSON-RPC -32602). Used for malformed modern `_meta`
/// envelopes: missing required fields or wrong types.
pub fn invalid_params(message: impl Into<String>, id: Option<Value>) -> Value {
    json_rpc_error(-32602, message, id)
}

/// Unsupported protocol version (`-32022` per 2026-07-28).
///
/// Returned when a modern request explicitly names a revision the server does
/// not implement. Lists all supported revisions so the client can retry with
/// a mutually supported version instead of silently entering legacy state.
pub fn unsupported_protocol_version(requested: &str, id: Option<Value>) -> Value {
    json_rpc_error_with_data(
        -32022,
        "Unsupported protocol version",
        Some(serde_json::json!({
            "supported": crate::mcp::runtime::SUPPORTED_PROTOCOL_VERSIONS,
            "requested": requested,
        })),
        id,
    )
}

/// Attach (or replace) the JSON-RPC `id` on an already-built error value.
///
/// `parse_modern_request_meta` builds envelope errors without an id; the
/// dispatcher patches the originating request id here so correlation is
/// preserved without threading ids through the parser.
pub fn with_request_id(mut error_value: Value, id: Option<Value>) -> Value {
    if let Some(obj) = error_value.as_object_mut() {
        obj.insert("id".to_string(), id.unwrap_or(Value::Null));
    }
    error_value
}

/// Build the reserved result `_meta` object carrying server identity.
///
/// Servers SHOULD include `io.modelcontextprotocol/serverInfo` on every
/// modern result. Identity is advisory (display/logging/debugging) and must
/// not drive behavior or security decisions.
pub fn server_info_meta() -> Value {
    serde_json::json!({
        crate::mcp::runtime::META_SERVER_INFO: {
            "name": crate::mcp::runtime::MCP_SERVER_NAME,
            "version": env!("CARGO_PKG_VERSION"),
        }
    })
}

/// Lifecycle error: method called before initialization.
pub fn not_initialized(method: &str, id: Option<Value>) -> Value {
    json_rpc_error_with_data(
        -32600,
        format!(
            "Server not initialized. Send 'initialize' request first (got '{}')",
            method
        ),
        Some(serde_json::json!({
            "code": "NOT_INITIALIZED",
            "expected_method": "initialize",
        })),
        id,
    )
}

/// Lifecycle error: duplicate initialize request.
pub fn already_initialized(id: Option<Value>) -> Value {
    json_rpc_error_with_data(
        -32600,
        "Server already initialized",
        Some(serde_json::json!({
            "code": "ALREADY_INITIALIZED",
        })),
        id,
    )
}

/// Unsupported-version response for a classification mismatch where the
/// inbound message was claim-less and therefore has no truthful requested
/// version to report.
pub fn unsupported_protocol_version_without_requested(id: Option<Value>) -> Value {
    json_rpc_error_with_data(
        -32022,
        "Unsupported protocol version",
        Some(serde_json::json!({
            "supported": crate::mcp::runtime::SUPPORTED_PROTOCOL_VERSIONS,
        })),
        id,
    )
}

// ═══════════════════════════════════════════════════════════════════════════════
// JSON-RPC types
// ═══════════════════════════════════════════════════════════════════════════════

#[derive(Deserialize, Debug)]
pub struct JsonRpcRequest {
    pub jsonrpc: String,
    pub method: String,
    pub params: Option<serde_json::Value>,
    pub id: Option<serde_json::Value>,
}

#[derive(Serialize, Debug)]
pub struct JsonRpcResponse {
    pub jsonrpc: String,
    pub id: Option<serde_json::Value>,
    pub result: serde_json::Value,
}

#[derive(Serialize, Debug)]
pub struct JsonRpcError {
    pub jsonrpc: String,
    pub id: Option<serde_json::Value>,
    pub error: JsonRpcErrorDetail,
}

#[derive(Serialize, Debug)]
pub struct JsonRpcErrorDetail {
    pub code: i32,
    pub message: String,
}

#[derive(Serialize, Debug)]
pub struct JsonRpcErrorWithData {
    pub jsonrpc: String,
    pub id: Option<serde_json::Value>,
    pub error: JsonRpcErrorDetailWithData,
}

#[derive(Serialize, Debug)]
pub struct JsonRpcErrorDetailWithData {
    pub code: i32,
    pub message: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data: Option<Value>,
}

// ═══════════════════════════════════════════════════════════════════════════════
// Initialize parameters (Workstream 1)
// ═══════════════════════════════════════════════════════════════════════════════

/// Client implementation info sent during initialize.
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ImplementationInfo {
    pub name: String,
    #[serde(default)]
    pub version: Option<String>,
}

/// Client capabilities declared during initialize.
///
/// Unknown fields are tolerated for forward compatibility; known fields are
/// type-checked.
#[derive(Deserialize, Debug, Default, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ClientCapabilities {
    /// Whether the client supports roots (file system roots).
    #[serde(default)]
    pub roots: Option<Value>,
    /// Whether the client supports sampling (LLM sampling requests).
    #[serde(default)]
    pub sampling: Option<Value>,
    /// Whether the client supports elicitation.
    #[serde(default)]
    pub elicitation: Option<Value>,
    /// Experimental capabilities (unknown shape — tolerated).
    #[serde(default)]
    pub experimental: Option<Value>,
}

/// Parameters for the `initialize` request.
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct InitializeParams {
    /// The MCP protocol version requested by the client.
    pub protocol_version: String,
    /// Client capabilities.
    #[serde(default)]
    pub capabilities: ClientCapabilities,
    /// Client implementation name and optional version.
    pub client_info: ImplementationInfo,
}

// ═══════════════════════════════════════════════════════════════════════════════
// Server capabilities (Workstream 4)
// ═══════════════════════════════════════════════════════════════════════════════

/// Server capabilities advertised in the initialize response.
#[derive(Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ServerCapabilities {
    /// Tool capabilities (list changed notifications).
    pub tools: ToolsCapability,
    /// eggsact-specific experimental extensions.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub experimental: Option<ExperimentalCapabilities>,
}

/// eggsact extension capabilities.
#[derive(Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ExperimentalCapabilities {
    /// eggsact-specific extensions.
    pub eggsact: EggsactExtensions,
}

/// eggsact extension flags.
#[derive(Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct EggsactExtensions {
    /// Whether profiles/list is supported.
    pub profiles: bool,
    /// Whether schema_detail filtering is supported.
    pub schema_detail: bool,
    /// Whether audience filtering is supported.
    pub audience_filtering: bool,
}

// ═══════════════════════════════════════════════════════════════════════════════
// Initialize result (revised)
// ═══════════════════════════════════════════════════════════════════════════════

#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct InitializeResult {
    pub protocol_version: String,
    pub capabilities: ServerCapabilities,
    pub server_info: ServerInfo,
    /// Concise cross-tool instructions (additive; few hundred bytes).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub instructions: Option<String>,
}

#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ServerInfo {
    pub name: String,
    pub version: String,
}

// ═══════════════════════════════════════════════════════════════════════════════
// Legacy compatibility types (preserved for backward compat)
// ═══════════════════════════════════════════════════════════════════════════════

/// Legacy `Capabilities` type for backward-compatible serialization.
/// Only used if code still references the old shape; prefer `ServerCapabilities`.
#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Capabilities {
    pub tools: ToolsCapability,
}

#[derive(Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ToolsCapability {
    pub list_changed: bool,
}

// ═══════════════════════════════════════════════════════════════════════════════
// Modern 2026-07-28 discovery (server/discover)
// ═══════════════════════════════════════════════════════════════════════════════

/// `server/discover` has no body parameters beyond the standard per-request
/// `_meta` envelope. This empty marker exists so the wire contract is typed;
/// dispatch reads `_meta` via `parse_modern_request_meta` instead.
#[derive(Deserialize, Debug, Default)]
pub struct DiscoverRequest {
    #[serde(default)]
    pub _meta: Option<Value>,
}

/// Modern discovery result per the final 2026-07-28 schema.
///
/// - `supportedVersions`: all revisions the server implements, in deterministic
///   preference order (modern first).
/// - `capabilities`: same tool capability shape as legacy initialize.
/// - `instructions`: concise cross-tool guidance (few hundred bytes).
/// - `ttlMs` / `cacheScope`: required cache hints for the modern result.
/// - `_meta[io.modelcontextprotocol/serverInfo]`: modern server identity; the
///   body carries no `serverInfo` member.
#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct DiscoverResult {
    pub result_type: String,
    pub supported_versions: Vec<String>,
    pub capabilities: ServerCapabilities,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub instructions: Option<String>,
    pub ttl_ms: u64,
    pub cache_scope: String,
    #[serde(rename = "_meta")]
    pub meta: Value,
}

impl DiscoverResult {
    pub fn current(capabilities: ServerCapabilities) -> Self {
        Self {
            result_type: "complete".to_string(),
            supported_versions: crate::mcp::runtime::SUPPORTED_PROTOCOL_VERSIONS
                .iter()
                .map(|s| s.to_string())
                .collect(),
            capabilities,
            instructions: Some(crate::mcp::runtime::SERVER_INSTRUCTIONS.to_string()),
            ttl_ms: crate::mcp::runtime::MODERN_CACHE_TTL_MS,
            cache_scope: crate::mcp::runtime::MODERN_CACHE_SCOPE.to_string(),
            meta: server_info_meta(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn deserialize_initialize_params_minimal() {
        let params: InitializeParams = serde_json::from_value(serde_json::json!({
            "protocolVersion": "2024-11-05",
            "capabilities": {},
            "clientInfo": {"name": "test-client"}
        }))
        .unwrap();
        assert_eq!(params.protocol_version, "2024-11-05");
        assert_eq!(params.client_info.name, "test-client");
        assert!(params.client_info.version.is_none());
    }

    #[test]
    fn deserialize_initialize_params_full() {
        let params: InitializeParams = serde_json::from_value(serde_json::json!({
            "protocolVersion": "2025-11-25",
            "capabilities": {
                "roots": {"listChanged": true},
                "sampling": {}
            },
            "clientInfo": {"name": "my-client", "version": "1.2.3"}
        }))
        .unwrap();
        assert_eq!(params.protocol_version, "2025-11-25");
        assert_eq!(params.client_info.name, "my-client");
        assert_eq!(params.client_info.version.as_deref(), Some("1.2.3"));
        assert!(params.capabilities.roots.is_some());
        assert!(params.capabilities.sampling.is_some());
    }

    #[test]
    fn deserialize_initialize_params_unknown_capabilities_tolerated() {
        let params: InitializeParams = serde_json::from_value(serde_json::json!({
            "protocolVersion": "2024-11-05",
            "capabilities": {"futureCapability": true},
            "clientInfo": {"name": "test"}
        }))
        .unwrap();
        assert_eq!(params.protocol_version, "2024-11-05");
    }

    #[test]
    fn initialize_result_serialization() {
        let result = InitializeResult {
            protocol_version: "2024-11-05".to_string(),
            capabilities: ServerCapabilities {
                tools: ToolsCapability {
                    list_changed: false,
                },
                experimental: Some(ExperimentalCapabilities {
                    eggsact: EggsactExtensions {
                        profiles: true,
                        schema_detail: true,
                        audience_filtering: true,
                    },
                }),
            },
            server_info: ServerInfo {
                name: "eggsact".to_string(),
                version: "1.1.5".to_string(),
            },
            instructions: Some("test instructions".to_string()),
        };
        let value = serde_json::to_value(&result).unwrap();
        assert_eq!(value["protocolVersion"], "2024-11-05");
        assert_eq!(value["capabilities"]["tools"]["listChanged"], false);
        assert_eq!(
            value["capabilities"]["experimental"]["eggsact"]["profiles"],
            true
        );
        assert_eq!(
            value["capabilities"]["experimental"]["eggsact"]["schemaDetail"],
            true
        );
        assert_eq!(
            value["capabilities"]["experimental"]["eggsact"]["audienceFiltering"],
            true
        );
        assert_eq!(value["serverInfo"]["name"], "eggsact");
    }

    #[test]
    fn lifecycle_error_structures() {
        let err = not_initialized("tools/list", Some(Value::Number(1.into())));
        let parsed: Value = serde_json::from_str(&err.to_string()).unwrap();
        assert_eq!(parsed["error"]["code"], -32600);
        assert!(parsed["error"]["message"]
            .as_str()
            .unwrap()
            .contains("not initialized"));
        let data = parsed["error"]["data"].as_object().unwrap();
        assert_eq!(data["code"], "NOT_INITIALIZED");

        let err = already_initialized(Some(Value::Number(2.into())));
        let parsed: Value = serde_json::from_str(&err.to_string()).unwrap();
        assert_eq!(parsed["error"]["code"], -32600);
        assert_eq!(parsed["error"]["data"]["code"], "ALREADY_INITIALIZED");
    }
}