tetrad 0.1.0

MCP de Consenso Quádruplo para Claude Code - Valida código usando Codex, Gemini e Qwen
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
//! Tipos do protocolo MCP (Model Context Protocol).
//!
//! O MCP usa JSON-RPC 2.0 como protocolo de transporte.
//! Este módulo define todos os tipos necessários para comunicação.

use serde::{Deserialize, Serialize};
use serde_json::Value;

// ═══════════════════════════════════════════════════════════════════════════
// Códigos de erro JSON-RPC padrão
// ═══════════════════════════════════════════════════════════════════════════

/// Erro de parse - JSON inválido.
pub const PARSE_ERROR: i32 = -32700;

/// Request inválida - JSON-RPC malformado.
pub const INVALID_REQUEST: i32 = -32600;

/// Método não encontrado.
pub const METHOD_NOT_FOUND: i32 = -32601;

/// Parâmetros inválidos.
pub const INVALID_PARAMS: i32 = -32602;

/// Erro interno do servidor.
pub const INTERNAL_ERROR: i32 = -32603;

// ═══════════════════════════════════════════════════════════════════════════
// Tipos básicos JSON-RPC
// ═══════════════════════════════════════════════════════════════════════════

/// ID de uma request JSON-RPC (pode ser número ou string).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(untagged)]
pub enum JsonRpcId {
    Number(i64),
    String(String),
}

impl From<i64> for JsonRpcId {
    fn from(n: i64) -> Self {
        JsonRpcId::Number(n)
    }
}

impl From<String> for JsonRpcId {
    fn from(s: String) -> Self {
        JsonRpcId::String(s)
    }
}

impl From<&str> for JsonRpcId {
    fn from(s: &str) -> Self {
        JsonRpcId::String(s.to_string())
    }
}

/// Request JSON-RPC 2.0.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcRequest {
    /// Versão do protocolo (sempre "2.0").
    pub jsonrpc: String,

    /// ID da request (opcional para notificações).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<JsonRpcId>,

    /// Nome do método a ser chamado.
    pub method: String,

    /// Parâmetros do método (opcional).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub params: Option<Value>,
}

impl JsonRpcRequest {
    /// Cria uma nova request.
    pub fn new(method: impl Into<String>, id: Option<JsonRpcId>) -> Self {
        Self {
            jsonrpc: "2.0".to_string(),
            id,
            method: method.into(),
            params: None,
        }
    }

    /// Adiciona parâmetros à request.
    pub fn with_params(mut self, params: Value) -> Self {
        self.params = Some(params);
        self
    }

    /// Verifica se é uma notificação (sem ID).
    pub fn is_notification(&self) -> bool {
        self.id.is_none()
    }
}

/// Response JSON-RPC 2.0.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcResponse {
    /// Versão do protocolo (sempre "2.0").
    pub jsonrpc: String,

    /// ID da request original.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<JsonRpcId>,

    /// Resultado em caso de sucesso.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub result: Option<Value>,

    /// Erro em caso de falha.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<JsonRpcError>,
}

impl JsonRpcResponse {
    /// Cria uma response de sucesso.
    pub fn success(id: Option<JsonRpcId>, result: Value) -> Self {
        Self {
            jsonrpc: "2.0".to_string(),
            id,
            result: Some(result),
            error: None,
        }
    }

    /// Cria uma response de erro.
    pub fn error(id: Option<JsonRpcId>, error: JsonRpcError) -> Self {
        Self {
            jsonrpc: "2.0".to_string(),
            id,
            result: None,
            error: Some(error),
        }
    }

    /// Verifica se a response é um erro.
    pub fn is_error(&self) -> bool {
        self.error.is_some()
    }
}

/// Erro JSON-RPC.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcError {
    /// Código do erro.
    pub code: i32,

    /// Mensagem de erro.
    pub message: String,

    /// Dados adicionais (opcional).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data: Option<Value>,
}

impl JsonRpcError {
    /// Cria um novo erro.
    pub fn new(code: i32, message: impl Into<String>) -> Self {
        Self {
            code,
            message: message.into(),
            data: None,
        }
    }

    /// Adiciona dados ao erro.
    pub fn with_data(mut self, data: Value) -> Self {
        self.data = Some(data);
        self
    }

    /// Erro de parse.
    pub fn parse_error() -> Self {
        Self::new(PARSE_ERROR, "Parse error")
    }

    /// Request inválida.
    pub fn invalid_request() -> Self {
        Self::new(INVALID_REQUEST, "Invalid Request")
    }

    /// Método não encontrado.
    pub fn method_not_found(method: &str) -> Self {
        Self::new(METHOD_NOT_FOUND, format!("Method not found: {}", method))
    }

    /// Parâmetros inválidos.
    pub fn invalid_params(message: impl Into<String>) -> Self {
        Self::new(INVALID_PARAMS, message)
    }

    /// Erro interno.
    pub fn internal_error(message: impl Into<String>) -> Self {
        Self::new(INTERNAL_ERROR, message)
    }
}

/// Notificação JSON-RPC (request sem ID, não espera resposta).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcNotification {
    /// Versão do protocolo.
    pub jsonrpc: String,

    /// Método.
    pub method: String,

    /// Parâmetros.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub params: Option<Value>,
}

impl JsonRpcNotification {
    /// Cria uma nova notificação.
    pub fn new(method: impl Into<String>) -> Self {
        Self {
            jsonrpc: "2.0".to_string(),
            method: method.into(),
            params: None,
        }
    }

    /// Adiciona parâmetros.
    pub fn with_params(mut self, params: Value) -> Self {
        self.params = Some(params);
        self
    }
}

// ═══════════════════════════════════════════════════════════════════════════
// Tipos MCP específicos
// ═══════════════════════════════════════════════════════════════════════════

/// Informações do servidor MCP.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ServerInfo {
    /// Nome do servidor.
    pub name: String,

    /// Versão do servidor.
    pub version: String,
}

impl Default for ServerInfo {
    fn default() -> Self {
        Self {
            name: "tetrad".to_string(),
            version: env!("CARGO_PKG_VERSION").to_string(),
        }
    }
}

/// Capacidades do servidor.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ServerCapabilities {
    /// Capacidades de ferramentas.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tools: Option<ToolsCapability>,
}

/// Capacidade de ferramentas.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct ToolsCapability {
    /// Suporta listagem de ferramentas.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub list_changed: Option<bool>,
}

/// Resultado da inicialização.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InitializeResult {
    /// Versão do protocolo suportada.
    pub protocol_version: String,

    /// Capacidades do servidor.
    pub capabilities: ServerCapabilities,

    /// Informações do servidor.
    pub server_info: ServerInfo,
}

impl Default for InitializeResult {
    fn default() -> Self {
        Self {
            protocol_version: "2024-11-05".to_string(),
            capabilities: ServerCapabilities {
                tools: Some(ToolsCapability::default()),
            },
            server_info: ServerInfo::default(),
        }
    }
}

/// Descrição de uma ferramenta MCP.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ToolDescription {
    /// Nome da ferramenta.
    pub name: String,

    /// Descrição da ferramenta.
    pub description: String,

    /// Schema de entrada (JSON Schema).
    pub input_schema: Value,
}

impl ToolDescription {
    /// Cria uma nova descrição de ferramenta.
    pub fn new(
        name: impl Into<String>,
        description: impl Into<String>,
        input_schema: Value,
    ) -> Self {
        Self {
            name: name.into(),
            description: description.into(),
            input_schema,
        }
    }
}

/// Resultado da listagem de ferramentas.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ListToolsResult {
    /// Lista de ferramentas disponíveis.
    pub tools: Vec<ToolDescription>,
}

/// Parâmetros para chamada de ferramenta.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CallToolParams {
    /// Nome da ferramenta.
    pub name: String,

    /// Argumentos da ferramenta.
    #[serde(default)]
    pub arguments: Value,
}

/// Conteúdo retornado por uma ferramenta.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum ToolContent {
    /// Conteúdo de texto.
    Text { text: String },
}

impl ToolContent {
    /// Cria conteúdo de texto.
    pub fn text(text: impl Into<String>) -> Self {
        ToolContent::Text { text: text.into() }
    }
}

/// Resultado de chamada de ferramenta.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ToolResult {
    /// Conteúdo retornado.
    pub content: Vec<ToolContent>,

    /// Se a chamada resultou em erro.
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub is_error: bool,
}

impl ToolResult {
    /// Cria um resultado de sucesso com texto.
    pub fn success(text: impl Into<String>) -> Self {
        Self {
            content: vec![ToolContent::text(text)],
            is_error: false,
        }
    }

    /// Cria um resultado de sucesso com JSON.
    pub fn success_json(value: &Value) -> Self {
        Self {
            content: vec![ToolContent::text(
                serde_json::to_string_pretty(value).unwrap_or_default(),
            )],
            is_error: false,
        }
    }

    /// Cria um resultado de erro.
    pub fn error(message: impl Into<String>) -> Self {
        Self {
            content: vec![ToolContent::text(message)],
            is_error: true,
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════
// Testes
// ═══════════════════════════════════════════════════════════════════════════

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

    #[test]
    fn test_json_rpc_id_number() {
        let id: JsonRpcId = 42.into();
        assert_eq!(id, JsonRpcId::Number(42));
    }

    #[test]
    fn test_json_rpc_id_string() {
        let id: JsonRpcId = "test-id".into();
        assert_eq!(id, JsonRpcId::String("test-id".to_string()));
    }

    #[test]
    fn test_json_rpc_request_serialize() {
        let request =
            JsonRpcRequest::new("test/method", Some(1.into())).with_params(json!({"key": "value"}));

        let json = serde_json::to_string(&request).unwrap();
        assert!(json.contains("\"jsonrpc\":\"2.0\""));
        assert!(json.contains("\"method\":\"test/method\""));
        assert!(json.contains("\"id\":1"));
    }

    #[test]
    fn test_json_rpc_request_deserialize() {
        let json = r#"{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}"#;
        let request: JsonRpcRequest = serde_json::from_str(json).unwrap();

        assert_eq!(request.jsonrpc, "2.0");
        assert_eq!(request.method, "initialize");
        assert_eq!(request.id, Some(JsonRpcId::Number(1)));
    }

    #[test]
    fn test_json_rpc_response_success() {
        let response = JsonRpcResponse::success(Some(1.into()), json!({"status": "ok"}));

        assert!(!response.is_error());
        assert!(response.result.is_some());
        assert!(response.error.is_none());
    }

    #[test]
    fn test_json_rpc_response_error() {
        let response =
            JsonRpcResponse::error(Some(1.into()), JsonRpcError::method_not_found("unknown"));

        assert!(response.is_error());
        assert!(response.result.is_none());
        assert!(response.error.is_some());
    }

    #[test]
    fn test_json_rpc_error_codes() {
        let parse_err = JsonRpcError::parse_error();
        assert_eq!(parse_err.code, PARSE_ERROR);

        let invalid_req = JsonRpcError::invalid_request();
        assert_eq!(invalid_req.code, INVALID_REQUEST);

        let method_err = JsonRpcError::method_not_found("test");
        assert_eq!(method_err.code, METHOD_NOT_FOUND);
    }

    #[test]
    fn test_tool_description() {
        let tool = ToolDescription::new(
            "test_tool",
            "A test tool",
            json!({
                "type": "object",
                "properties": {
                    "input": { "type": "string" }
                }
            }),
        );

        assert_eq!(tool.name, "test_tool");
        assert_eq!(tool.description, "A test tool");
    }

    #[test]
    fn test_tool_result_success() {
        let result = ToolResult::success("Operation completed");
        assert!(!result.is_error);
        assert_eq!(result.content.len(), 1);
    }

    #[test]
    fn test_tool_result_error() {
        let result = ToolResult::error("Something went wrong");
        assert!(result.is_error);
    }

    #[test]
    fn test_initialize_result_default() {
        let result = InitializeResult::default();
        assert_eq!(result.server_info.name, "tetrad");
        assert!(result.capabilities.tools.is_some());
    }

    #[test]
    fn test_notification() {
        let notif = JsonRpcNotification::new("initialized");
        assert_eq!(notif.method, "initialized");
        assert!(notif.params.is_none());
    }
}