diff --git a/crates/tinymcp/src/registry/sources/official/index_tests.rs b/crates/tinymcp/src/registry/sources/official/index_tests.rs index bfd926ee..8b691039 100644 --- a/crates/tinymcp/src/registry/sources/official/index_tests.rs +++ b/crates/tinymcp/src/registry/sources/official/index_tests.rs @@ -851,8 +851,10 @@ async fn an_uncurated_server_the_registry_does_not_list_is_still_an_error() { .await .unwrap_err(); + // Not described from a curated entry, and not a raw upstream failure: + // the registry's 404 is the unknown-server answer (#167). assert!( - matches!(error, Error::Http { status: 404, .. }), + matches!(&error, Error::UnknownServer { server } if server == "io.example/nowhere"), "{error:?}" ); } diff --git a/crates/tinymcp/src/registry/sources/official/mod.rs b/crates/tinymcp/src/registry/sources/official/mod.rs index 5ad51918..41e35717 100644 --- a/crates/tinymcp/src/registry/sources/official/mod.rs +++ b/crates/tinymcp/src/registry/sources/official/mod.rs @@ -282,8 +282,8 @@ impl McpOfficialRegistry { /// /// # Errors /// - /// Returns [`Error::UnknownServer`] when the registry lists no version of - /// it, plus whatever the upstream returns. + /// Returns [`Error::UnknownServer`] when the registry does not list it (a + /// 404, or no version), plus whatever the upstream returns. pub(super) async fn get( &self, store: &Store, @@ -327,9 +327,19 @@ impl McpOfficialRegistry { base_url(auth), encode_path_segment(qualified_name) ); - let body = self + // The registry answers 404 for a name it does not list. That is the + // same answer as an empty versions document, not an upstream failure. + let body = match self .send(self.request(auth, &url), &url, RegistryOperation::Detail) - .await?; + .await + { + Err(Error::Http { status: 404, .. }) => { + return Err(Error::UnknownServer { + server: qualified_name.to_string(), + }); + } + sent => sent?, + }; let document: Value = serde_json::from_str(&body) .map_err(|error| Error::malformed(format!("official versions response: {error}")))?; @@ -429,7 +439,9 @@ impl McpOfficialRegistry { .request(auth, &url) .query(&[("limit", limit.to_string())]) .query(&[("version", "latest")]); - if !query.is_empty() { + // The registry refuses a search shorter than its minimum with a 422, + // so a query that short is served as the unfiltered page instead. + if query.trim().chars().count() >= MIN_SEARCH_CHARS { request = request.query(&[("search", query)]); } if let Some(cursor) = cursor { @@ -517,6 +529,11 @@ fn list_url(auth: &McpRegistryAuthConfig) -> String { format!("{}/v0/servers", base_url(auth)) } +/// The shortest `search` the official registry accepts, counted after +/// trimming. A shorter one is answered `422 search must contain at least 3 +/// characters after trimming`. +const MIN_SEARCH_CHARS: usize = 3; + /// The cache key for one page of one search. fn search_cache_key(query: &str, page: u32, page_size: u32) -> String { format!("mcp_official:search:latest:{query}:{page}:{page_size}") diff --git a/crates/tinymcp/src/registry/sources/official/mod_tests.rs b/crates/tinymcp/src/registry/sources/official/mod_tests.rs index db711e7c..a0b4be9d 100644 --- a/crates/tinymcp/src/registry/sources/official/mod_tests.rs +++ b/crates/tinymcp/src/registry/sources/official/mod_tests.rs @@ -884,6 +884,7 @@ struct Seen { pages: AtomicUsize, cursors: Mutex>>, versions: Mutex>>, + searches: Mutex>>, authorization: Mutex>, detail_path: Mutex>, } @@ -930,6 +931,7 @@ async fn paged_registry(pages: usize) -> (String, Arc) { let cursor = param(&uri, "cursor"); seen.cursors.lock().push(cursor.clone()); seen.versions.lock().push(param(&uri, "version")); + seen.searches.lock().push(param(&uri, "search")); let page: usize = cursor .as_deref() @@ -1017,6 +1019,23 @@ async fn the_first_page_is_fetched_without_a_cursor() { assert_eq!(seen.cursors.lock().as_slice(), &[None]); } +#[tokio::test] +async fn a_query_shorter_than_the_registry_minimum_is_not_sent_as_a_search() { + let (base, seen) = paged_registry(1).await; + + for query in ["a", " ab ", "abc"] { + adapter() + .search(&store(), &auth_at(&base), &cursors(), query, 1, 20) + .await + .expect("the search succeeds"); + } + + assert_eq!( + seen.searches.lock().as_slice(), + &[None, None, Some("abc".to_string())] + ); +} + #[tokio::test] async fn every_page_asks_for_the_latest_version_only() { let (base, seen) = paged_registry(3).await; @@ -1353,6 +1372,25 @@ async fn a_name_containing_a_slash_is_sent_as_one_escaped_segment() { ); } +#[tokio::test] +async fn a_name_the_registry_does_not_list_is_an_unknown_server() { + let base = failing_registry( + 404, + r#"{"title":"Not Found","status":404,"detail":"Server not found"}"#, + ) + .await; + + let error = adapter() + .get(&store(), &auth_at(&base), "@acme/nothing") + .await + .expect_err("not listed"); + + match error { + Error::UnknownServer { server } => assert_eq!(server, "@acme/nothing"), + other => panic!("expected an unknown-server error, got {other:?}"), + } +} + #[tokio::test] async fn a_registry_listing_no_versions_reports_an_unknown_server() { let app = Router::new().fallback(get(|| async { axum::Json(json!({ "servers": [] })) }));