Merge pull request #2590 from dotCooCoo/feat/craft-mcp-validated-instructions

craft-mcp: validated initialize returns the session's instructions
This commit is contained in:
BFlat
2026-11-26 10:02:53 +07:00
committed by GitHub
2 changed files with 27 additions and 9 deletions
+3 -1
View File
@@ -10,7 +10,9 @@ initialization, initialization can occur in a batch, and client information need
to be an object. `Policy::Strict` requires initialization first (except ping),
rejects batched or repeated initialization, and requires client name and version.
Both respond with the sole supported version, 2025-03-26, even if the client offered
a different version. An initialized notification moves the session to `Ready`.
a different version. Instructions set with `Session::set_instructions` are returned by
every initialize response, in either policy and with or without legacy initialization.
An initialized notification moves the session to `Ready`.
This crate does not enforce every MCP lifecycle or capability rule; applications
must validate any additional requirements they depend on.
+24 -8
View File
@@ -106,12 +106,9 @@ impl Session {
let version = asked
.filter(|v| self.versions.iter().any(|supported| supported == v))
.unwrap_or_else(|| self.versions.first().map(String::as_str).unwrap_or(PROTOCOL_VERSION));
let mut result = json!({"protocolVersion": version, "capabilities": self.capabilities, "serverInfo": self.server_info});
if let Some(instructions) = &self.instructions {
result["instructions"] = json!(instructions);
}
let result = json!({"protocolVersion": version, "capabilities": self.capabilities, "serverInfo": self.server_info});
self.state = State::AwaitingInitialized;
return Some(Ok(result));
return Some(Ok(self.with_instructions(result)));
}
let Some(params) = request.params.and_then(Value::as_object) else {
return Some(Err(invalid_initialize()));
@@ -126,9 +123,8 @@ impl Session {
return Some(Err(invalid_initialize()));
}
self.state = State::AwaitingInitialized;
return Some(Ok(
json!({"protocolVersion": PROTOCOL_VERSION, "capabilities": self.capabilities, "serverInfo": {"name": self.name, "version": self.version}}),
));
let result = json!({"protocolVersion": PROTOCOL_VERSION, "capabilities": self.capabilities, "serverInfo": {"name": self.name, "version": self.version}});
return Some(Ok(self.with_instructions(result)));
}
if self.policy == Policy::Strict && self.state == State::New {
return Some(Err(RpcError::new(-32600, "Server not initialized")));
@@ -136,6 +132,13 @@ impl Session {
None
}
fn with_instructions(&self, mut result: Value) -> Value {
if let Some(instructions) = &self.instructions {
result["instructions"] = json!(instructions);
}
result
}
/// Observe a JSON-RPC notification; notifications never receive replies.
pub fn notification(&mut self, request: Request<'_>) {
if request.method == "notifications/initialized" {
@@ -231,6 +234,19 @@ mod tests {
assert_eq!(result["capabilities"], json!({"resources": {"subscribe": true}}));
}
#[test]
fn validated_initialization_returns_instructions() {
let p = json!({"protocolVersion":PROTOCOL_VERSION, "capabilities":{}, "clientInfo":{"name":"client", "version":"1"}});
for policy in [Policy::Compatible, Policy::Strict] {
let mut s = Session::new("test", "1", policy);
assert!(s.request(call("initialize", Some(&p)), false).unwrap().unwrap().get("instructions").is_none());
let mut s = Session::new("test", "1", policy);
s.set_instructions("help".into());
let result = s.request(call("initialize", Some(&p)), false).unwrap().unwrap();
assert_eq!(result["instructions"], "help");
}
}
#[test]
fn malformed_initialize_does_not_change_state() {
let mut s = Session::new("test", "1", Policy::Strict);