Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(mcp): handle missing resource URI in resource listing

Steven Enamakel2b23d5cfcf59
diff --git a/crates/openhuman-core/src/mcp/server/resources.rs b/crates/openhuman-core/src/mcp/server/resources.rs
index 185360fccf..c0b5a4506e 100644
--- a/crates/openhuman-core/src/mcp/server/resources.rs
+++ b/crates/openhuman-core/src/mcp/server/resources.rs
@@ -20,6 +20,10 @@
//! a matching catalog entry fails that test and therefore CI.
use serde_json::{json, Value};
+use tinymcp::{ResourceSpec, ToolCallError};
+
+/// Every bundled prompt is markdown.
+const MIME_TYPE: &str = "text/markdown";
struct PromptResource {
uri: &'static str,
@@ -175,62 +179,30 @@ const RESOURCE_CATALOG: &[PromptResource] = &[
},
];
-/// Returns the `resources/list` result payload listing every catalog entry.
-pub fn list_resources_result() -> Value {
- let resources: Vec<Value> = RESOURCE_CATALOG
+/// The catalog as `resources/list` advertises it.
+pub fn resource_specs() -> Vec<ResourceSpec> {
+ let resources = RESOURCE_CATALOG
.iter()
.map(|r| {
- json!({
- "uri": r.uri,
- "name": r.name,
- "description": r.description,
- "mimeType": "text/markdown"
- })
+ ResourceSpec::new(r.uri, r.name)
+ .with_description(r.description)
+ .with_mime_type(MIME_TYPE)
})
- .collect();
+ .collect::<Vec<_>>();
log::debug!("[mcp_server] resources/list count={}", resources.len());
- json!({ "resources": resources })
-}
-
-/// Returns the `resources/templates/list` result payload.
-///
-/// The catalog is fully static — every URI is concrete, none are templated —
-/// so the response is always an empty `resourceTemplates` array. The handler
-/// exists so MCP clients that probe `resources/templates/list` after seeing
-/// the `resources` capability get a well-formed result instead of
-/// `-32601 Method not found`.
-pub fn list_resource_templates_result() -> Value {
- log::debug!("[mcp_server] resources/templates/list count=0 (catalog is static)");
- json!({ "resourceTemplates": [] })
+ resources
}
-/// Returns the `resources/read` result payload for the given URI, or a JSON-RPC
-/// error value when the URI is unknown (`-32002`) or missing (`-32602`).
-pub fn read_resource_result(params: &Value) -> Result<Value, (i64, &'static str, String)> {
- let uri = params
- .as_object()
- .and_then(|obj| obj.get("uri"))
- .and_then(Value::as_str)
- .map(str::trim)
- .filter(|uri| !uri.is_empty())
- .ok_or_else(|| {
- (
- -32602_i64,
- "Invalid params",
- "resources/read params.uri must be a non-empty string".to_string(),
- )
- })?;
-
+/// The `resources/read` result for `uri`, or
+/// [`ToolCallError::ResourceNotFound`] (`-32002`) when the catalog has no such
+/// entry. `tinymcp` has already rejected a missing or blank `uri`.
+pub fn read_resource(uri: &str) -> Result<Value, ToolCallError> {
let resource = RESOURCE_CATALOG
.iter()
.find(|r| r.uri == uri)
.ok_or_else(|| {
log::debug!("[mcp_server] resources/read unknown uri={uri}");
- (
- -32002_i64,
- "Resource not found",
- format!("no resource with uri `{uri}`"),
- )
+ ToolCallError::ResourceNotFound(format!("no resource with uri `{uri}`"))
})?;
log::debug!(
@@ -241,7 +213,7 @@ pub fn read_resource_result(params: &Value) -> Result<Value, (i64, &'static str,
Ok(json!({
"contents": [{
"uri": resource.uri,
- "mimeType": "text/markdown",
+ "mimeType": MIME_TYPE,
"text": resource.content
}]
}))
diff --git a/crates/openhuman-core/src/mcp/server/resources_tests.rs b/crates/openhuman-core/src/mcp/server/resources_tests.rs
index 8ad4f63d15..d0395617c9 100644
--- a/crates/openhuman-core/src/mcp/server/resources_tests.rs
+++ b/crates/openhuman-core/src/mcp/server/resources_tests.rs
@@ -30,30 +30,23 @@ fn catalog_mirrors_builtins() {
#[test]
fn list_resources_returns_all_catalog_entries() {
- let result = list_resources_result();
- let resources = result["resources"].as_array().expect("resources array");
+ let resources = resource_specs();
assert_eq!(
resources.len(),
RESOURCE_CATALOG.len(),
"resources/list count mismatch"
);
- // Every entry has required fields
- for entry in resources {
- assert!(entry["uri"].is_string(), "uri must be string");
- assert!(entry["name"].is_string(), "name must be string");
- assert_eq!(entry["mimeType"], "text/markdown");
+ for entry in &resources {
+ assert!(!entry.uri.is_empty(), "uri must be set");
+ assert!(!entry.name.is_empty(), "name must be set");
+ assert_eq!(entry.mime_type.as_deref(), Some("text/markdown"));
}
}
#[test]
fn list_resources_includes_core_and_agent_uris() {
- let result = list_resources_result();
- let uris: Vec<&str> = result["resources"]
- .as_array()
- .unwrap()
- .iter()
- .map(|r| r["uri"].as_str().unwrap())
- .collect();
+ let resources = resource_specs();
+ let uris: Vec<&str> = resources.iter().map(|r| r.uri.as_str()).collect();
for expected in [
"openhuman://prompts/identity",
"openhuman://prompts/soul",
@@ -67,8 +60,7 @@ fn list_resources_includes_core_and_agent_uris() {
#[test]
fn read_resource_returns_content_for_known_uri() {
- let params = json!({ "uri": "openhuman://prompts/identity" });
- let result = read_resource_result(&params).expect("should succeed");
+ let result = read_resource("openhuman://prompts/identity").expect("should succeed");
let contents = result["contents"].as_array().expect("contents array");
assert_eq!(contents.len(), 1);
assert_eq!(contents[0]["uri"], "openhuman://prompts/identity");
@@ -78,17 +70,10 @@ fn read_resource_returns_content_for_known_uri() {
#[test]
fn read_resource_returns_minus_32002_for_unknown_uri() {
- let params = json!({ "uri": "openhuman://prompts/agents/nonexistent" });
- let err = read_resource_result(&params).expect_err("should fail for unknown URI");
- assert_eq!(err.0, -32002);
- assert!(err.2.contains("nonexistent"));
-}
-
-#[test]
-fn read_resource_returns_minus_32602_for_missing_uri() {
- let params = json!({});
- let err = read_resource_result(&params).expect_err("should fail without uri");
- assert_eq!(err.0, -32602);
+ let err = read_resource("openhuman://prompts/agents/nonexistent")
+ .expect_err("should fail for unknown URI");
+ assert_eq!(err.code(), -32002);
+ assert!(err.message().contains("nonexistent"));
}
#[test]
@@ -96,8 +81,7 @@ fn read_resource_returns_content_for_each_subagent() {
use crate::agent::registry::agents::BUILTINS;
for b in BUILTINS {
let uri = format!("openhuman://prompts/agents/{}", b.id);
- let params = json!({ "uri": uri });
- let result = read_resource_result(&params)
+ let result = read_resource(&uri)
.unwrap_or_else(|_| panic!("read_resource failed for agent `{}`", b.id));
let text = result["contents"][0]["text"].as_str().unwrap_or("");
assert!(
@@ -108,18 +92,6 @@ fn read_resource_returns_content_for_each_subagent() {
}
}
-#[test]
-fn list_resource_templates_returns_empty_array() {
- let result = list_resource_templates_result();
- let templates = result["resourceTemplates"]
- .as_array()
- .expect("resourceTemplates must be a JSON array");
- assert!(
- templates.is_empty(),
- "resources/templates/list must return an empty array — the catalog is static"
- );
-}
-
#[test]
fn all_catalog_uris_are_unique() {
let mut uris: Vec<&str> = RESOURCE_CATALOG.iter().map(|r| r.uri).collect();