agent-sdk-toolkit 0.1.0-alpha.3

Optional tool-pack helpers layered over agent-sdk-core primitive contracts.
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
//! Scripted MCP protocol harnesses for SDK consumers. Use these fakes to test
//! tool/resource/prompt lifecycle and host filtering without live MCP servers.
//! Harness methods mutate in-memory endpoints and scripted response state only.
//!
use std::collections::{BTreeMap, BTreeSet};

use agent_sdk_core::{AgentError, AgentErrorKind, RetryClassification};
use serde_json::{Value, json};

use crate::protocol::{
    JsonRpcFrame, JsonRpcId, JsonRpcLineEndpoint, JsonRpcRequest, JsonRpcResponse, expect_response,
    protocol_violation,
};

enum ReceiveNext {
    Empty,
    Handled,
    Frame(JsonRpcFrame),
}

#[derive(Clone, Debug, Default)]
/// In-memory scripted mcp server fixture for SDK conformance tests.
/// Use it to script deterministic behavior in memory; any transcript or endpoint mutation is documented on the method that performs it.
pub struct ScriptedMcpServer {
    tools: BTreeMap<String, Value>,
    resources: BTreeMap<String, String>,
    prompts: BTreeMap<String, Value>,
    initialized: bool,
    next_client_request: i64,
}

impl ScriptedMcpServer {
    /// Creates a new testing::protocol::mcp value with explicit
    /// caller-provided inputs. This constructor is data-only and
    /// performs no I/O or external side effects.
    pub fn new() -> Self {
        Self::default()
    }

    /// Returns an updated testing::protocol::mcp value with tool applied.
    /// This is data construction only and does not execute the configured
    /// behavior.
    pub fn tool(mut self, name: impl Into<String>, result: Value) -> Self {
        self.tools.insert(name.into(), result);
        self
    }

    /// Returns an updated value with resource configured.
    /// This configures or reads the scripted protocol mock in memory.
    pub fn resource(mut self, uri: impl Into<String>, text: impl Into<String>) -> Self {
        self.resources.insert(uri.into(), text.into());
        self
    }

    /// Adds a scripted prompt definition and returns the updated fake server.
    /// This mutates only the builder's in-memory prompt map; request frames are appended later
    /// when a client asks the endpoint for prompts.
    pub fn prompt(mut self, name: impl Into<String>, spec: Value) -> Self {
        self.prompts.insert(name.into(), spec);
        self
    }

    /// Handle next.
    /// This consumes one queued JSON-RPC frame from the in-memory endpoint and mutates only
    /// scripted mock state.
    pub fn handle_next(&mut self, endpoint: &JsonRpcLineEndpoint) -> Result<bool, AgentError> {
        let frame = match receive_frame_or_parse_error(endpoint)? {
            ReceiveNext::Empty => return Ok(false),
            ReceiveNext::Handled => return Ok(true),
            ReceiveNext::Frame(frame) => frame,
        };
        let request = match frame {
            JsonRpcFrame::Request(request) => request,
            JsonRpcFrame::Notification(notification) => {
                if notification.method == "notifications/initialized" {
                    self.initialized = true;
                }
                return Ok(true);
            }
            JsonRpcFrame::Response(_) => {
                endpoint.send_error(None, -32600, "MCP server expected a request frame")?;
                return Ok(true);
            }
        };
        match request.method.as_str() {
            "initialize" => {
                endpoint.send_result(
                    request.id,
                    json!({
                        "protocolVersion": request.params
                            .get("protocolVersion")
                            .cloned()
                            .unwrap_or_else(|| json!("2025-11-25")),
                        "capabilities": {
                            "tools": {"listChanged": false},
                            "resources": {"subscribe": false, "listChanged": false},
                            "prompts": {"listChanged": false}
                        },
                        "serverInfo": {
                            "name": "agent-sdk-toolkit-mcp-fake",
                            "version": "0.0.0"
                        }
                    }),
                )?;
            }
            "tools/list" => {
                if !self.ensure_initialized(endpoint, &request)? {
                    return Ok(true);
                }
                endpoint.send_result(request.id, json!({"tools": self.tool_list()}))?;
            }
            "tools/call" => {
                if !self.ensure_initialized(endpoint, &request)? {
                    return Ok(true);
                }
                self.handle_tool_call(endpoint, request)?;
            }
            "resources/list" => {
                if !self.ensure_initialized(endpoint, &request)? {
                    return Ok(true);
                }
                endpoint.send_result(request.id, json!({"resources": self.resource_list()}))?;
            }
            "resources/read" => {
                if !self.ensure_initialized(endpoint, &request)? {
                    return Ok(true);
                }
                self.handle_resource_read(endpoint, request)?;
            }
            "prompts/list" => {
                if !self.ensure_initialized(endpoint, &request)? {
                    return Ok(true);
                }
                endpoint.send_result(request.id, json!({"prompts": self.prompt_list()}))?;
            }
            "logging/setLevel" => {
                if !self.ensure_initialized(endpoint, &request)? {
                    return Ok(true);
                }
                endpoint.send_error(
                    Some(request.id),
                    -32010,
                    "MCP logging control denied by default policy",
                )?;
            }
            _ => {
                endpoint.send_error(Some(request.id), -32601, "MCP method not found")?;
            }
        };
        Ok(true)
    }

    /// Request sampling.
    /// This appends the corresponding JSON-RPC frame to the in-memory test endpoint transcript.
    pub fn request_sampling(
        &mut self,
        endpoint: &JsonRpcLineEndpoint,
    ) -> Result<JsonRpcId, AgentError> {
        let id = self.next_client_request_id();
        endpoint
            .send_request(
                id.clone(),
                "sampling/createMessage",
                json!({"messages": [{"role": "user", "content": {"type": "text", "text": "sample"}}]}),
            )
            .map(|_| id)
    }

    /// Request elicitation.
    /// This appends the corresponding JSON-RPC frame to the in-memory test endpoint transcript.
    pub fn request_elicitation(
        &mut self,
        endpoint: &JsonRpcLineEndpoint,
    ) -> Result<JsonRpcId, AgentError> {
        let id = self.next_client_request_id();
        endpoint
            .send_request(
                id.clone(),
                "elicitation/create",
                json!({"message": "need user input"}),
            )
            .map(|_| id)
    }

    /// Returns the response currently held by this value.
    /// This reads scripted protocol state or a queued response without contacting an external
    /// process.
    pub fn response(&self, endpoint: &JsonRpcLineEndpoint) -> Result<JsonRpcResponse, AgentError> {
        expect_response(endpoint.receive_frame()?)
    }

    /// Returns the initialized currently held by this value.
    /// This reads scripted protocol state or a queued response without contacting an external
    /// process.
    pub fn initialized(&self) -> bool {
        self.initialized
    }

    fn ensure_initialized(
        &self,
        endpoint: &JsonRpcLineEndpoint,
        request: &JsonRpcRequest,
    ) -> Result<bool, AgentError> {
        if self.initialized {
            return Ok(true);
        }
        endpoint.send_error(
            Some(request.id.clone()),
            -32002,
            "MCP client must send notifications/initialized before normal operation",
        )?;
        Ok(false)
    }

    fn next_client_request_id(&mut self) -> JsonRpcId {
        let id = JsonRpcId::Number(self.next_client_request);
        self.next_client_request += 1;
        id
    }

    fn tool_list(&self) -> Vec<Value> {
        self.tools
            .keys()
            .map(|name| json!({"name": name, "inputSchema": {"type": "object"}}))
            .collect()
    }

    fn resource_list(&self) -> Vec<Value> {
        self.resources
            .keys()
            .map(|uri| json!({"uri": uri, "mimeType": "text/plain"}))
            .collect()
    }

    fn prompt_list(&self) -> Vec<Value> {
        self.prompts
            .iter()
            .map(|(name, spec)| {
                let mut spec = spec.clone();
                if let Some(object) = spec.as_object_mut() {
                    object.insert("name".to_string(), json!(name));
                    return spec;
                }
                json!({"name": name})
            })
            .collect()
    }

    fn handle_tool_call(
        &self,
        endpoint: &JsonRpcLineEndpoint,
        request: JsonRpcRequest,
    ) -> Result<(), AgentError> {
        let name = request
            .params
            .get("name")
            .and_then(Value::as_str)
            .ok_or_else(|| protocol_violation("MCP tools/call requires name"))?;
        let Some(result) = self.tools.get(name) else {
            endpoint.send_error(Some(request.id), -32602, "MCP tool is not available")?;
            return Ok(());
        };
        endpoint.send_result(
            request.id,
            json!({
                "content": [{"type": "text", "text": result.to_string()}],
                "isError": false
            }),
        )?;
        Ok(())
    }

    fn handle_resource_read(
        &self,
        endpoint: &JsonRpcLineEndpoint,
        request: JsonRpcRequest,
    ) -> Result<(), AgentError> {
        let uri = request
            .params
            .get("uri")
            .and_then(Value::as_str)
            .ok_or_else(|| protocol_violation("MCP resources/read requires uri"))?;
        let Some(text) = self.resources.get(uri) else {
            endpoint.send_error(Some(request.id), -32602, "MCP resource is not available")?;
            return Ok(());
        };
        endpoint.send_result(
            request.id,
            json!({
                "contents": [{
                    "uri": uri,
                    "mimeType": "text/plain",
                    "text": text
                }]
            }),
        )?;
        Ok(())
    }
}

#[derive(Clone, Debug)]
/// In-memory mcp host proxy fixture for SDK conformance tests.
/// Use it to script deterministic behavior in memory; any transcript or endpoint mutation is documented on the method that performs it.
pub struct McpHostProxy {
    endpoint: JsonRpcLineEndpoint,
    allowed_tools: BTreeSet<String>,
    allowed_resources: BTreeSet<String>,
    allowed_prompts: BTreeSet<String>,
    pending_methods: BTreeMap<String, String>,
    next_id: i64,
}

impl McpHostProxy {
    /// Creates a new testing::protocol::mcp value with explicit
    /// caller-provided inputs. This constructor is data-only and
    /// performs no I/O or external side effects.
    pub fn new(endpoint: JsonRpcLineEndpoint) -> Self {
        Self {
            endpoint,
            allowed_tools: BTreeSet::new(),
            allowed_resources: BTreeSet::new(),
            allowed_prompts: BTreeSet::new(),
            pending_methods: BTreeMap::new(),
            next_id: 1,
        }
    }

    /// Returns an updated value with allow tool configured.
    /// This configures or reads the scripted protocol mock in memory.
    pub fn allow_tool(mut self, name: impl Into<String>) -> Self {
        self.allowed_tools.insert(name.into());
        self
    }

    /// Returns an updated value with allow resource configured.
    /// This updates the scripted MCP mock allowlist in memory for later protocol calls.
    pub fn allow_resource(mut self, uri: impl Into<String>) -> Self {
        self.allowed_resources.insert(uri.into());
        self
    }

    /// Returns an updated value with allow prompt configured.
    /// This updates the scripted MCP mock allowlist in memory for later protocol calls.
    pub fn allow_prompt(mut self, name: impl Into<String>) -> Self {
        self.allowed_prompts.insert(name.into());
        self
    }

    /// Returns the endpoint currently held by this value.
    /// This reads scripted protocol state or a queued response without contacting an external
    /// process.
    pub fn endpoint(&self) -> &JsonRpcLineEndpoint {
        &self.endpoint
    }

    /// Initialize.
    /// This appends the corresponding JSON-RPC frame to the scripted MCP mock transcript and
    /// returns the request id when applicable.
    pub fn initialize(&mut self) -> Result<JsonRpcId, AgentError> {
        self.request(
            "initialize",
            json!({
                "protocolVersion": "2025-11-25",
                "capabilities": {},
                "clientInfo": {
                    "name": "agent-sdk-toolkit",
                    "version": "0.0.0"
                }
            }),
        )
    }

    /// Sends the MCP `notifications/initialized` frame to the scripted server endpoint.
    /// This appends one in-memory JSON-RPC notification; it does not contact a live MCP process or
    /// mutate SDK runtime state.
    pub fn initialized(&self) -> Result<(), AgentError> {
        self.endpoint
            .send_notification("notifications/initialized", json!({}))
            .map(|_| ())
    }

    /// List tools.
    /// This appends the corresponding JSON-RPC frame to the in-memory test endpoint transcript.
    pub fn list_tools(&mut self) -> Result<JsonRpcId, AgentError> {
        self.request("tools/list", json!({}))
    }

    /// List resources.
    /// This appends the corresponding JSON-RPC frame to the scripted MCP mock transcript and
    /// returns the request id when applicable.
    pub fn list_resources(&mut self) -> Result<JsonRpcId, AgentError> {
        self.request("resources/list", json!({}))
    }

    /// List prompts.
    /// This appends the corresponding JSON-RPC frame to the scripted MCP mock transcript and
    /// returns the request id when applicable.
    pub fn list_prompts(&mut self) -> Result<JsonRpcId, AgentError> {
        self.request("prompts/list", json!({}))
    }

    /// Call tool.
    /// This appends the corresponding JSON-RPC frame to the in-memory test endpoint transcript.
    pub fn call_tool(&mut self, name: &str, arguments: Value) -> Result<JsonRpcId, AgentError> {
        if !self.allowed_tools.contains(name) {
            return Err(policy_denial(format!(
                "MCP tool {name} is not selected in host proxy policy"
            )));
        }
        self.request("tools/call", json!({"name": name, "arguments": arguments}))
    }

    /// Read resource.
    /// This appends the corresponding JSON-RPC frame to the in-memory test endpoint transcript.
    pub fn read_resource(&mut self, uri: &str) -> Result<JsonRpcId, AgentError> {
        if !self.allowed_resources.contains(uri) {
            return Err(policy_denial(format!(
                "MCP resource {uri} is not selected in host proxy policy"
            )));
        }
        self.request("resources/read", json!({"uri": uri}))
    }

    /// Handle next.
    /// This consumes one queued JSON-RPC frame from the endpoint and mutates only the scripted
    /// mock state.
    pub fn handle_next(&self, endpoint: &JsonRpcLineEndpoint) -> Result<bool, AgentError> {
        let frame = match receive_frame_or_parse_error(endpoint)? {
            ReceiveNext::Empty => return Ok(false),
            ReceiveNext::Handled => return Ok(true),
            ReceiveNext::Frame(frame) => frame,
        };
        let JsonRpcFrame::Request(request) = frame else {
            return Ok(true);
        };
        match request.method.as_str() {
            "sampling/createMessage" | "elicitation/create" => endpoint.send_error(
                Some(request.id),
                -32010,
                "MCP server-to-client request denied by host proxy policy",
            )?,
            _ => endpoint.send_error(Some(request.id), -32601, "MCP client method not found")?,
        };
        Ok(true)
    }

    /// Returns the response currently held by this value.
    /// This reads scripted protocol state or a queued response without contacting an external
    /// process.
    pub fn response(&mut self) -> Result<JsonRpcResponse, AgentError> {
        let mut response = expect_response(self.endpoint.receive_frame()?)?;
        if response.id == JsonRpcId::Null {
            return Ok(response);
        }
        let Some(method) = self.pending_methods.remove(&response.id.as_key()) else {
            return Err(protocol_violation("unexpected MCP response id"));
        };
        self.apply_response_policy(&method, &mut response);
        Ok(response)
    }

    /// Returns allowed tool names from response for the current value.
    /// This is a read-only or data-construction helper unless the method body explicitly calls
    /// a port or store.
    pub fn allowed_tool_names_from_response(
        &self,
        response: &JsonRpcResponse,
    ) -> Result<Vec<String>, AgentError> {
        let tools = response
            .result
            .as_ref()
            .and_then(|value| value.get("tools"))
            .and_then(Value::as_array)
            .ok_or_else(|| protocol_violation("MCP tools/list response missing tools array"))?;
        let mut names = tools
            .iter()
            .filter_map(|tool| tool.get("name").and_then(Value::as_str))
            .filter(|name| self.allowed_tools.contains(*name))
            .map(str::to_string)
            .collect::<Vec<_>>();
        names.sort();
        Ok(names)
    }

    fn request(&mut self, method: &str, params: Value) -> Result<JsonRpcId, AgentError> {
        let id = JsonRpcId::Number(self.next_id);
        self.next_id += 1;
        self.endpoint.send_request(id.clone(), method, params)?;
        self.pending_methods.insert(id.as_key(), method.to_string());
        Ok(id)
    }

    fn apply_response_policy(&self, method: &str, response: &mut JsonRpcResponse) {
        if response.error.is_some() {
            return;
        }
        let Some(result) = response.result.as_mut() else {
            return;
        };
        match method {
            "tools/list" => filter_named_array(result, "tools", "name", &self.allowed_tools),
            "resources/list" => {
                filter_named_array(result, "resources", "uri", &self.allowed_resources)
            }
            "prompts/list" => filter_named_array(result, "prompts", "name", &self.allowed_prompts),
            _ => {}
        }
    }
}

fn receive_frame_or_parse_error(endpoint: &JsonRpcLineEndpoint) -> Result<ReceiveNext, AgentError> {
    let Some(line) = endpoint.try_receive_raw_line()? else {
        return Ok(ReceiveNext::Empty);
    };
    match JsonRpcFrame::from_line(&line) {
        Ok(frame) => Ok(ReceiveNext::Frame(frame)),
        Err(error) => {
            endpoint.send_error(None, -32700, error.context().message)?;
            Ok(ReceiveNext::Handled)
        }
    }
}

fn filter_named_array(result: &mut Value, field: &str, key: &str, allowed: &BTreeSet<String>) {
    let Some(items) = result.get_mut(field).and_then(Value::as_array_mut) else {
        return;
    };
    items.retain(|item| {
        item.get(key)
            .and_then(Value::as_str)
            .is_some_and(|name| allowed.contains(name))
    });
}

fn policy_denial(message: impl Into<String>) -> AgentError {
    AgentError::new(
        AgentErrorKind::PolicyDenial,
        RetryClassification::UserActionNeeded,
        message,
    )
}