mod cache;
pub mod http;
pub mod registry;
pub mod skills_sh;
mod tap;
#[cfg(test)]
mod tests;
use std::path::Path;
use std::time::{Duration, SystemTime};
use serde::{Deserialize, Serialize};
use crate::adapters::detect::NoRegistry;
use crate::env::Env;
use crate::error::{CoreError, ErrorCode};
use crate::fs::Fs;
use crate::local_settings::LocalSettings;
use crate::mcp_source::{self, RegistryRequest, ResolveRequest, Resolved};
use crate::model::TomlDocument;
pub use crate::model::catalogs::{
BUILTIN_IDS, CatalogRecord, CatalogSourceKind, CatalogsFile, is_catalog_id,
registry_url_problem,
};
use crate::model::server::ServerSource;
use crate::paths::SurjectionHome;
use crate::remote::git::Runner;
use crate::skills::install::{InstallRequest, Source};
use crate::skills::{LinkKind, SkillsEnv};
use crate::sources::SourceRef;
use crate::store::{CommitId, Operation, Store, StoreRead};
pub use http::{Http, HttpLimits, HttpResponse, UreqHttp};
pub const CATALOGS_FILE: &str = "catalogs.toml";
pub const ENTITY: &str = "catalogs";
pub const CACHE_DIR: &str = "catalogs";
pub const TTL: Duration = Duration::from_secs(24 * 60 * 60);
pub const MIN_QUERY: usize = 3;
pub const REF_PREFIX: &str = "catalog:";
pub const REGISTRY_ATTRIBUTION: &str = "Data from the Official MCP Registry";
pub const BUILTIN_REGISTRY: &str = "mcp-registry";
pub const BUILTIN_SKILLS: &str = "anthropic-skills";
pub const BUILTIN_SKILLS_SH: &str = "skills-sh";
pub const REGISTRY_URL: &str = "https://registry.modelcontextprotocol.io";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CatalogKind {
McpRegistry,
GitTap,
SkillsSh,
}
impl CatalogKind {
pub const fn as_str(self) -> &'static str {
match self {
CatalogKind::McpRegistry => "mcp_registry",
CatalogKind::GitTap => "git_tap",
CatalogKind::SkillsSh => "skills_sh",
}
}
}
impl From<CatalogSourceKind> for CatalogKind {
fn from(kind: CatalogSourceKind) -> Self {
match kind {
CatalogSourceKind::McpRegistry => CatalogKind::McpRegistry,
CatalogSourceKind::GitTap => CatalogKind::GitTap,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Catalog {
pub id: String,
pub kind: CatalogKind,
pub title: String,
pub url: String,
pub git_ref: Option<String>,
pub enabled: bool,
pub builtin: bool,
pub official: bool,
pub attribution: Option<String>,
}
impl Catalog {
fn exclude(&self) -> &'static [&'static str] {
if self.builtin && self.id == BUILTIN_SKILLS {
&["template", "spec"]
} else {
&[]
}
}
}
fn builtins() -> [Catalog; 3] {
[
Catalog {
id: BUILTIN_REGISTRY.to_owned(),
kind: CatalogKind::McpRegistry,
title: "Official MCP Registry".to_owned(),
url: REGISTRY_URL.to_owned(),
git_ref: None,
enabled: true,
builtin: true,
official: false,
attribution: Some(REGISTRY_ATTRIBUTION.to_owned()),
},
Catalog {
id: BUILTIN_SKILLS.to_owned(),
kind: CatalogKind::GitTap,
title: "Anthropic skills".to_owned(),
url: "https://github.com/anthropics/skills".to_owned(),
git_ref: None,
enabled: true,
builtin: true,
official: true,
attribution: None,
},
Catalog {
id: BUILTIN_SKILLS_SH.to_owned(),
kind: CatalogKind::SkillsSh,
title: "skills.sh".to_owned(),
url: "https://skills.sh".to_owned(),
git_ref: None,
enabled: true,
builtin: true,
official: false,
attribution: None,
},
]
}
fn load_doc(read: &dyn StoreRead) -> Result<TomlDocument<CatalogsFile>, CoreError> {
let Some(text) = read.read_file(CATALOGS_FILE)? else {
return Ok(TomlDocument::new(CatalogsFile::new()));
};
let path = Path::new(CATALOGS_FILE);
let doc = TomlDocument::<CatalogsFile>::parse(&text)
.map_err(|diagnostic| diagnostic.to_core_error(Some(path)))?;
if let Some(err) = doc.value().validate().first_error(Some(path)) {
return Err(err);
}
Ok(doc)
}
pub fn list(read: Option<&dyn StoreRead>) -> Result<Vec<Catalog>, CoreError> {
let file = match read {
Some(read) => load_doc(read)?.into_value(),
None => CatalogsFile::new(),
};
let mut catalogs: Vec<Catalog> = builtins().into_iter().collect();
for catalog in &mut catalogs {
if let Some(record) = file.catalogs.get(&catalog.id) {
catalog.enabled = record.enabled.unwrap_or(true);
}
}
for (id, record) in &file.catalogs {
if BUILTIN_IDS.contains(&id.as_str()) {
continue;
}
let (Some(kind), Some(url)) = (record.kind, record.url.clone()) else {
continue;
};
let official = kind == CatalogSourceKind::GitTap && tap::is_anthropic(&url);
catalogs.push(Catalog {
id: id.clone(),
kind: kind.into(),
title: record.title.clone().unwrap_or_else(|| id.clone()),
url: url.trim_end_matches('/').to_owned(),
git_ref: record.git_ref.clone(),
enabled: record.enabled.unwrap_or(true),
builtin: false,
official,
attribution: None,
});
}
Ok(catalogs)
}
fn find(read: Option<&dyn StoreRead>, id: &str) -> Result<Catalog, CoreError> {
list(read)?
.into_iter()
.find(|catalog| catalog.id == id)
.ok_or_else(|| CoreError::new(ErrorCode::NotFound, format!("there is no catalog {id:?}")))
}
fn change(
store: &Store<'_>,
op: &str,
summary: String,
edit: impl FnOnce(&mut CatalogsFile) -> Result<bool, CoreError>,
) -> Result<Option<CommitId>, CoreError> {
let operation = Operation::new(op, summary).entity(ENTITY);
let committed = store.transaction(operation, |tx| {
let mut doc = load_doc(tx)?;
if !edit(doc.value_mut())? {
return Ok(());
}
let path = Path::new(CATALOGS_FILE);
if let Some(err) = doc.value().validate().first_error(Some(path)) {
return Err(err);
}
let text = doc.to_text()?;
tx.write(CATALOGS_FILE, text)
})?;
Ok(committed.commit)
}
#[derive(Debug, Clone)]
pub struct NewCatalog {
pub kind: CatalogSourceKind,
pub url: String,
pub id: Option<String>,
pub git_ref: Option<String>,
pub title: Option<String>,
}
fn id_from(text: &str) -> String {
let id: String = text
.to_lowercase()
.chars()
.map(|c| {
if c.is_ascii_lowercase() || c.is_ascii_digit() || matches!(c, '.' | '_' | '-') {
c
} else {
'-'
}
})
.collect();
let id = id.trim_matches(|c: char| !c.is_ascii_alphanumeric());
let id: String = id.chars().take(64).collect();
if id.is_empty() {
"catalog".to_owned()
} else {
id
}
}
fn default_id(new: &NewCatalog) -> String {
match new.kind {
CatalogSourceKind::McpRegistry => url::Url::parse(&new.url)
.ok()
.and_then(|url| url.host_str().map(id_from))
.unwrap_or_else(|| "registry".to_owned()),
CatalogSourceKind::GitTap => id_from(
new.url
.trim_end_matches(['/', '\\'])
.rsplit(['/', '\\', ':'])
.next()
.unwrap_or("tap")
.trim_end_matches(".git"),
),
}
}
pub fn add(store: &Store<'_>, new: &NewCatalog) -> Result<(String, Option<CommitId>), CoreError> {
let url = new.url.trim().trim_end_matches('/').to_owned();
let existing = list(Some(store))?;
if let Some(same) = existing
.iter()
.find(|catalog| catalog.url.eq_ignore_ascii_case(&url))
{
return Err(CoreError::new(
ErrorCode::AlreadyExists,
format!("this catalog is already there as {:?}", same.id),
));
}
let taken = |id: &str| existing.iter().any(|catalog| catalog.id == id);
let id = match &new.id {
Some(id) => {
if !is_catalog_id(id) {
return Err(CoreError::new(
ErrorCode::InvalidId,
format!(
"{id:?} is not a catalog id (1 to 64 characters of a-z, 0-9, '.', '_', \
'-', starting with a letter or digit)"
),
));
}
if taken(id) {
return Err(CoreError::new(
ErrorCode::AlreadyExists,
format!("there is already a catalog {id:?}"),
));
}
id.clone()
}
None => {
let base = default_id(new);
let mut id = base.clone();
let mut n = 2;
while taken(&id) {
id = format!("{base}-{n}");
n += 1;
}
id
}
};
let record = CatalogRecord {
kind: Some(new.kind),
url: Some(url),
git_ref: new
.git_ref
.as_deref()
.map(str::trim)
.filter(|r| !r.is_empty())
.map(str::to_owned),
title: new
.title
.as_deref()
.map(str::trim)
.filter(|t| !t.is_empty())
.map(str::to_owned),
enabled: None,
extra: toml::Table::new(),
};
let commit = change(store, "catalog.add", format!("catalog: add {id}"), |file| {
file.catalogs.insert(id.clone(), record);
Ok(true)
})?;
Ok((id, commit))
}
pub fn remove(
store: &Store<'_>,
sx: &SkillsEnv<'_>,
id: &str,
) -> Result<Option<CommitId>, CoreError> {
if BUILTIN_IDS.contains(&id) {
return Err(CoreError::new(
ErrorCode::InvalidValue,
format!("{id:?} is a built-in catalog: turn it off instead"),
));
}
find(Some(store), id)?;
let commit = change(
store,
"catalog.remove",
format!("catalog: remove {id}"),
|file| Ok(file.catalogs.remove(id).is_some()),
)?;
cache::clear(sx, id)?;
Ok(commit)
}
pub fn set_enabled(
store: &Store<'_>,
id: &str,
enabled: bool,
) -> Result<Option<CommitId>, CoreError> {
find(Some(store), id)?;
let verb = if enabled { "enable" } else { "disable" };
change(
store,
&format!("catalog.{verb}"),
format!("catalog: {verb} {id}"),
|file| {
let builtin = BUILTIN_IDS.contains(&id);
let current = file
.catalogs
.get(id)
.and_then(|record| record.enabled)
.unwrap_or(true);
if current == enabled {
return Ok(false);
}
if builtin {
if enabled {
file.catalogs.remove(id);
} else {
file.catalogs
.insert(id.to_owned(), CatalogRecord::switch(false));
}
} else if let Some(record) = file.catalogs.get_mut(id) {
record.enabled = (!enabled).then_some(false);
}
Ok(true)
},
)
}
pub fn host(catalog: &Catalog) -> Option<String> {
match catalog.kind {
CatalogKind::GitTap => match SourceRef::from_git_url(&catalog.url) {
Ok(source) if source.key().starts_with("file:") => None,
Ok(source) => Some(
crate::sources::without_insecure_prefix(source.key())
.split('/')
.next()
.unwrap_or_default()
.to_owned(),
),
Err(_) => None,
},
_ => Some(http::host_of(&catalog.url)),
}
}
pub fn contacts(catalogs: &[Catalog]) -> Vec<String> {
let mut hosts: Vec<String> = Vec::new();
for host in catalogs
.iter()
.filter(|catalog| catalog.enabled)
.filter_map(host)
{
if !hosts.contains(&host) {
hosts.push(host);
}
}
hosts
}
pub const LEGACY_HOSTS: [&str; 2] = ["registry.modelcontextprotocol.io", "github.com"];
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OnlineSetting {
pub on: bool,
pub answered: bool,
pub hosts: Vec<String>,
pub not_now: Vec<String>,
}
impl OnlineSetting {
fn of(settings: &LocalSettings) -> Self {
let on = settings.online_catalogs == Some(true);
let hosts = match &settings.online_catalog_hosts {
Some(hosts) => hosts.clone(),
None if on => LEGACY_HOSTS.map(str::to_owned).to_vec(),
None => Vec::new(),
};
Self {
on,
answered: settings.online_catalogs.is_some(),
hosts,
not_now: settings
.online_catalog_hosts_not_now
.clone()
.unwrap_or_default(),
}
}
pub fn allows(&self, host: &str) -> bool {
self.on && self.hosts.iter().any(|approved| approved == host)
}
pub fn unapproved(&self, contacts: &[String]) -> Vec<String> {
contacts
.iter()
.filter(|host| !self.hosts.contains(host))
.cloned()
.collect()
}
pub fn to_ask(&self, contacts: &[String]) -> Vec<String> {
if !self.on {
return Vec::new();
}
self.unapproved(contacts)
.into_iter()
.filter(|host| !self.not_now.contains(host))
.collect()
}
pub fn decided(&self, contacts: &[String]) -> bool {
self.answered && self.to_ask(contacts).is_empty()
}
pub fn gate(&self, catalog: &Catalog) -> Result<(), CoreError> {
if !self.on {
return Err(off_error());
}
match host(catalog) {
Some(host) if !self.allows(&host) => Err(not_approved_error(&host)),
_ => Ok(()),
}
}
}
pub fn online(fs: &dyn Fs, home: &SurjectionHome) -> Result<OnlineSetting, CoreError> {
let settings = LocalSettings::load(fs, &home.state_dir())?;
Ok(OnlineSetting::of(&settings))
}
fn with_hosts(mut list: Vec<String>, hosts: &[String]) -> Vec<String> {
for host in hosts {
if !list.contains(host) {
list.push(host.clone());
}
}
list
}
pub fn set_online(
fs: &dyn Fs,
home: &SurjectionHome,
on: bool,
contacts: &[String],
) -> Result<(), CoreError> {
let state = home.state_dir();
let mut settings = LocalSettings::load(fs, &state)?;
let before = OnlineSetting::of(&settings);
settings.online_catalogs = Some(on);
if on {
settings.online_catalog_hosts = Some(with_hosts(before.hosts, contacts));
let not_now: Vec<String> = before
.not_now
.into_iter()
.filter(|host| !contacts.contains(host))
.collect();
settings.online_catalog_hosts_not_now = (!not_now.is_empty()).then_some(not_now);
}
settings.save(fs, &state)
}
pub fn answer_hosts(
fs: &dyn Fs,
home: &SurjectionHome,
hosts: &[String],
allow: bool,
) -> Result<(), CoreError> {
let state = home.state_dir();
let mut settings = LocalSettings::load(fs, &state)?;
let before = OnlineSetting::of(&settings);
let not_now = if allow {
settings.online_catalog_hosts = Some(with_hosts(before.hosts, hosts));
before
.not_now
.into_iter()
.filter(|host| !hosts.contains(host))
.collect()
} else {
let declined: Vec<String> = hosts
.iter()
.filter(|host| !before.hosts.contains(host))
.cloned()
.collect();
with_hosts(before.not_now, &declined)
};
settings.online_catalog_hosts_not_now = (!not_now.is_empty()).then_some(not_now);
settings.save(fs, &state)
}
fn off_error() -> CoreError {
CoreError::new(
ErrorCode::CatalogsOff,
"online catalogs are off on this machine, so no catalog was contacted; turn them on in \
Install > Browse or with `surject catalog online on`",
)
}
fn not_approved_error(host: &str) -> CoreError {
CoreError::new(
ErrorCode::CatalogHostWaiting,
format!(
"Surjection has not contacted {host}: it waits for your OK; allow it in Settings > \
Catalogs or with `surject catalog online on`"
),
)
}
fn require_online(fs: &dyn Fs, home: &SurjectionHome) -> Result<(), CoreError> {
if online(fs, home)?.on {
Ok(())
} else {
Err(off_error())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum EntryKind {
Server,
Skill,
Plugin,
}
impl EntryKind {
pub const fn as_str(self) -> &'static str {
match self {
EntryKind::Server => "server",
EntryKind::Skill => "skill",
EntryKind::Plugin => "plugin",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Publisher {
pub name: String,
pub checked: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EntryPackage {
pub registry_type: String,
pub identifier: String,
pub version: Option<String>,
pub transport: Option<String>,
pub pinned: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EntryRemote {
pub transport: String,
pub url: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PluginSourceKind {
Relative,
Github,
Url,
GitSubdir,
Npm,
Archive,
Command,
Unknown,
}
impl PluginSourceKind {
pub const fn as_str(self) -> &'static str {
match self {
PluginSourceKind::Relative => "relative",
PluginSourceKind::Github => "github",
PluginSourceKind::Url => "url",
PluginSourceKind::GitSubdir => "git_subdir",
PluginSourceKind::Npm => "npm",
PluginSourceKind::Archive => "archive",
PluginSourceKind::Command => "command",
PluginSourceKind::Unknown => "unknown",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PluginSource {
pub kind: PluginSourceKind,
pub url: Option<String>,
pub path: Option<String>,
pub git_ref: Option<String>,
pub sha: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CatalogEntry {
pub catalog: String,
pub id: String,
pub kind: EntryKind,
pub name: String,
pub description: Option<String>,
pub version: Option<String>,
pub updated_at: Option<String>,
pub deprecated: bool,
pub status_message: Option<String>,
pub publisher: Option<Publisher>,
pub official: bool,
pub warning: Option<String>,
pub repository: Option<String>,
pub website: Option<String>,
pub license: Option<String>,
pub transports: Vec<String>,
pub packages: Vec<EntryPackage>,
pub remotes: Vec<EntryRemote>,
pub required_secrets: Vec<String>,
pub pinned: bool,
pub git_ref: Option<String>,
pub commit: Option<String>,
pub path: Option<String>,
pub skills: Vec<String>,
pub servers: Vec<String>,
pub not_imported: Vec<String>,
pub source: Option<PluginSource>,
pub install_mcp: bool,
pub install_skill: bool,
pub problem: Option<String>,
pub server_json: Option<String>,
#[serde(default)]
pub mcp_servers: Option<String>,
#[serde(default)]
pub installs: Option<u64>,
}
impl CatalogEntry {
pub fn reference(&self) -> String {
format!("{REF_PREFIX}{}/{}", self.catalog, self.id)
}
}
pub(crate) fn is_plugin_name(name: &str) -> bool {
!name.is_empty()
&& name.len() <= 128
&& name.starts_with(|c: char| c.is_ascii_alphanumeric())
&& name
.chars()
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-'))
&& !name.contains("..")
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ResultStatus {
Fresh,
Cached,
Stale,
Off,
Disabled,
QueryTooShort,
Error,
}
impl ResultStatus {
pub const fn as_str(self) -> &'static str {
match self {
ResultStatus::Fresh => "fresh",
ResultStatus::Cached => "cached",
ResultStatus::Stale => "stale",
ResultStatus::Off => "off",
ResultStatus::Disabled => "disabled",
ResultStatus::QueryTooShort => "query_too_short",
ResultStatus::Error => "error",
}
}
}
#[derive(Debug, Clone)]
pub struct Results {
pub catalog: String,
pub status: ResultStatus,
pub fetched_at: Option<SystemTime>,
pub age: Option<Duration>,
pub error: Option<CoreError>,
pub entries: Vec<CatalogEntry>,
pub next_cursor: Option<String>,
pub attribution: Option<String>,
}
impl Results {
fn new(catalog: &Catalog, status: ResultStatus) -> Self {
Self {
catalog: catalog.id.clone(),
status,
fetched_at: None,
age: None,
error: None,
entries: Vec::new(),
next_cursor: None,
attribution: catalog.attribution.clone(),
}
}
}
pub struct CatalogEnv<'a> {
pub sx: &'a SkillsEnv<'a>,
pub http: &'a dyn Http,
}
static NO_REGISTRY: NoRegistry = NoRegistry;
#[derive(Clone, Copy)]
struct Parts<'a> {
env: &'a Env,
fs: &'a dyn Fs,
home: &'a SurjectionHome,
runner: &'a dyn Runner,
link_kind: LinkKind,
http: &'a dyn Http,
}
impl<'a> Parts<'a> {
fn of(cx: &CatalogEnv<'a>) -> Self {
Self {
env: cx.sx.env,
fs: cx.sx.fs,
home: cx.sx.home,
runner: cx.sx.runner,
link_kind: cx.sx.link_kind,
http: cx.http,
}
}
fn sx(&self) -> SkillsEnv<'a> {
SkillsEnv {
env: self.env,
fs: self.fs,
home: self.home,
registry: &NO_REGISTRY,
runner: self.runner,
link_kind: self.link_kind,
}
}
}
struct Got<T> {
data: Option<T>,
status: ResultStatus,
fetched_at: Option<SystemTime>,
age: Option<Duration>,
error: Option<CoreError>,
}
fn cached_or<T: Serialize + serde::de::DeserializeOwned>(
parts: &Parts<'_>,
gate: &Result<(), CoreError>,
refetch: bool,
catalog: &str,
name: &str,
key: &str,
fetch: impl FnOnce() -> Result<T, CoreError>,
) -> Got<T> {
let sx = parts.sx();
let now = parts.env.now();
let hit = (!refetch)
.then(|| cache::read::<T>(&sx, catalog, name, key))
.flatten();
let from_hit = |hit: cache::Hit<T>, status: ResultStatus, error: Option<CoreError>| Got {
age: Some(hit.age(now)),
fetched_at: Some(hit.fetched_at),
data: Some(hit.data),
status,
error,
};
if let Err(closed) = gate {
return match hit {
Some(hit) => {
let waiting =
(closed.code() == ErrorCode::CatalogHostWaiting).then(|| closed.clone());
from_hit(hit, ResultStatus::Off, waiting)
}
None => Got {
data: None,
status: ResultStatus::Off,
fetched_at: None,
age: None,
error: Some(closed.clone()),
},
};
}
let hit = match hit {
Some(hit) if hit.age(now) < TTL => return from_hit(hit, ResultStatus::Cached, None),
hit => hit,
};
match fetch() {
Ok(data) => {
let fetched_at = cache::write(&sx, catalog, name, key, &data).ok();
Got {
data: Some(data),
status: ResultStatus::Fresh,
fetched_at: fetched_at.or(Some(now)),
age: Some(Duration::ZERO),
error: None,
}
}
Err(err) => {
let stale = hit.or_else(|| cache::read::<T>(&sx, catalog, name, key));
match stale {
Some(hit) => from_hit(hit, ResultStatus::Stale, Some(err)),
None => Got {
data: None,
status: ResultStatus::Error,
fetched_at: None,
age: None,
error: Some(err),
},
}
}
}
}
fn search_prefix(catalog: &Catalog) -> String {
format!("search\n{}\n", catalog.url)
}
fn search_key(catalog: &Catalog, needle: &str, cursor: Option<&str>) -> String {
format!(
"{}{needle}\n{}",
search_prefix(catalog),
cursor.unwrap_or_default()
)
}
fn tap_key(catalog: &Catalog) -> String {
format!(
"tap\n{}\n{}",
catalog.url,
catalog.git_ref.as_deref().unwrap_or_default()
)
}
fn tap_index(
parts: &Parts<'_>,
gate: &Result<(), CoreError>,
refetch: bool,
catalog: &Catalog,
) -> Got<tap::TapIndex> {
cached_or(
parts,
gate,
refetch,
&catalog.id,
"index",
&tap_key(catalog),
|| tap::index(&parts.sx(), catalog, catalog.exclude()),
)
}
fn with<T>(
mut results: Results,
got: Got<T>,
entries: impl FnOnce(T) -> (Vec<CatalogEntry>, Option<String>),
) -> Results {
results.status = got.status;
results.fetched_at = got.fetched_at;
results.age = got.age;
results.error = got.error;
if let Some(data) = got.data {
(results.entries, results.next_cursor) = entries(data);
}
results
}
fn search_catalog(
parts: &Parts<'_>,
gate: &Result<(), CoreError>,
catalog: &Catalog,
query: &str,
cursor: Option<&str>,
) -> Results {
if !catalog.enabled {
return Results::new(catalog, ResultStatus::Disabled);
}
crate::diagnostics::log::event(
"catalog.search",
&[
("catalog", &catalog.id),
("query_len", &query.trim().chars().count()),
],
);
let needle = query.trim().to_lowercase();
match catalog.kind {
CatalogKind::SkillsSh => {
if query.trim().chars().count() < skills_sh::MIN_QUERY {
let mut results = Results::new(catalog, ResultStatus::QueryTooShort);
results.error = Some(CoreError::new(
ErrorCode::InvalidValue,
format!(
"search {} with at least {} characters",
catalog.title,
skills_sh::MIN_QUERY
),
));
return results;
}
let key = search_key(catalog, &needle, None);
let name = format!("search-{}", cache::digest(&key));
let got = cached_or(parts, gate, false, &catalog.id, &name, &key, || {
skills_sh::search(parts.http, catalog, query).map(|entries| (entries, None))
});
with(Results::new(catalog, ResultStatus::Fresh), got, |data| data)
}
CatalogKind::McpRegistry => {
if query.trim().chars().count() < MIN_QUERY {
let mut results = Results::new(catalog, ResultStatus::QueryTooShort);
results.error = Some(CoreError::new(
ErrorCode::InvalidValue,
format!(
"search {} with at least {MIN_QUERY} characters",
catalog.title
),
));
return results;
}
let key = search_key(catalog, &needle, cursor);
let name = format!("search-{}", cache::digest(&key));
let got = cached_or(parts, gate, false, &catalog.id, &name, &key, || {
registry::search(parts.http, catalog, query, cursor)
.map(|page| (page.entries, page.next_cursor))
});
with(Results::new(catalog, ResultStatus::Fresh), got, |data| data)
}
CatalogKind::GitTap => {
let got = tap_index(parts, gate, false, catalog);
with(Results::new(catalog, ResultStatus::Fresh), got, |index| {
let entries = index
.entries
.into_iter()
.filter(|entry| registry::matches(entry, &needle))
.collect();
(entries, None)
})
}
}
}
pub fn search(
cx: &CatalogEnv<'_>,
read: Option<&dyn StoreRead>,
query: &str,
only: Option<&str>,
) -> Result<Vec<Results>, CoreError> {
let catalogs: Vec<Catalog> = match only {
Some(id) => vec![find(read, id)?],
None => list(read)?
.into_iter()
.filter(|catalog| catalog.enabled)
.collect(),
};
let parts = Parts::of(cx);
let setting = online(parts.fs, parts.home)?;
let setting = &setting;
Ok(std::thread::scope(|scope| {
let handles: Vec<_> = catalogs
.iter()
.map(|catalog| {
scope.spawn(move || {
search_catalog(&parts, &setting.gate(catalog), catalog, query, None)
})
})
.collect();
handles
.into_iter()
.zip(&catalogs)
.map(|(handle, catalog)| {
handle.join().unwrap_or_else(|_| {
let mut results = Results::new(catalog, ResultStatus::Error);
results.error = Some(CoreError::new(
ErrorCode::CatalogUnreachable,
"the search of this catalog stopped unexpectedly",
));
results
})
})
.collect()
}))
}
pub fn search_one(
cx: &CatalogEnv<'_>,
read: Option<&dyn StoreRead>,
catalog: &str,
query: &str,
cursor: Option<&str>,
) -> Result<Results, CoreError> {
let catalog = find(read, catalog)?;
let parts = Parts::of(cx);
let gate = online(parts.fs, parts.home)?.gate(&catalog);
Ok(search_catalog(&parts, &gate, &catalog, query, cursor))
}
#[derive(Debug, Clone)]
pub struct Shown {
pub entry: CatalogEntry,
pub status: ResultStatus,
pub age: Option<Duration>,
pub error: Option<CoreError>,
}
fn shown(got: Got<CatalogEntry>) -> Result<Shown, CoreError> {
match got.data {
Some(entry) => Ok(Shown {
entry,
status: got.status,
age: got.age,
error: got.error,
}),
None => Err(got.error.unwrap_or_else(off_error)),
}
}
fn not_in(catalog: &Catalog, id: &str) -> CoreError {
CoreError::new(
ErrorCode::NotFound,
format!("{} lists no entry {id:?}", catalog.title),
)
}
pub fn show(
cx: &CatalogEnv<'_>,
read: Option<&dyn StoreRead>,
catalog: &str,
entry: &str,
) -> Result<Shown, CoreError> {
let catalog = find(read, catalog)?;
let parts = Parts::of(cx);
let gate = online(parts.fs, parts.home)?.gate(&catalog);
match catalog.kind {
CatalogKind::SkillsSh => {
let hit =
cache::entry_in_pages(&parts.sx(), &catalog.id, &search_prefix(&catalog), entry)
.ok_or_else(|| {
CoreError::new(
ErrorCode::NotFound,
format!(
"{} has no saved result {entry:?}: search for it first",
catalog.title
),
)
})?;
let age = hit.age(parts.env.now());
Ok(Shown {
status: if gate.is_err() {
ResultStatus::Off
} else if age >= TTL {
ResultStatus::Stale
} else {
ResultStatus::Cached
},
age: Some(age),
entry: hit.data,
error: gate
.err()
.filter(|closed| closed.code() == ErrorCode::CatalogHostWaiting),
})
}
CatalogKind::McpRegistry => {
let key = format!("entry\n{}\n{entry}", catalog.url);
let name = format!("entry-{}", cache::digest(&key));
shown(cached_or(
&parts,
&gate,
false,
&catalog.id,
&name,
&key,
|| registry::entry(parts.http, &catalog, entry, None),
))
}
CatalogKind::GitTap => {
let got = tap_index(&parts, &gate, false, &catalog);
let found = got
.data
.as_ref()
.map(|index| index.entries.iter().find(|e| e.id == entry).cloned());
match found {
Some(Some(found)) => Ok(Shown {
entry: found,
status: got.status,
age: got.age,
error: got.error,
}),
Some(None) => Err(not_in(&catalog, entry)),
None => Err(got.error.unwrap_or_else(off_error)),
}
}
}
}
pub fn cached_entry(
sx: &SkillsEnv<'_>,
read: Option<&dyn StoreRead>,
catalog: &str,
entry: &str,
) -> Result<CatalogEntry, CoreError> {
let catalog = find(read, catalog)?;
match catalog.kind {
CatalogKind::SkillsSh => {
cache::entry_in_pages(sx, &catalog.id, &search_prefix(&catalog), entry)
.map(|hit| hit.data)
.ok_or_else(|| not_in(&catalog, entry))
}
CatalogKind::GitTap => {
cache::read::<tap::TapIndex>(sx, &catalog.id, "index", &tap_key(&catalog))
.and_then(|hit| hit.data.entries.into_iter().find(|e| e.id == entry))
.ok_or_else(|| not_in(&catalog, entry))
}
CatalogKind::McpRegistry => {
let key = format!("entry\n{}\n{entry}", catalog.url);
if let Some(hit) = cache::read::<CatalogEntry>(
sx,
&catalog.id,
&format!("entry-{}", cache::digest(&key)),
&key,
) {
return Ok(hit.data);
}
cache::entry_in_pages(sx, &catalog.id, &search_prefix(&catalog), entry)
.map(|hit| hit.data)
.ok_or_else(|| not_in(&catalog, entry))
}
}
}
pub fn refresh(
cx: &CatalogEnv<'_>,
read: Option<&dyn StoreRead>,
only: Option<&str>,
) -> Result<Vec<Results>, CoreError> {
let catalogs: Vec<Catalog> = match only {
Some(id) => vec![find(read, id)?],
None => list(read)?
.into_iter()
.filter(|catalog| catalog.enabled)
.collect(),
};
let parts = Parts::of(cx);
let setting = online(parts.fs, parts.home)?;
if !setting.on {
return Err(off_error());
}
let setting = &setting;
Ok(std::thread::scope(|scope| {
let handles: Vec<_> = catalogs
.iter()
.map(|catalog| {
scope.spawn(move || {
let sx = parts.sx();
let base = Results::new(catalog, ResultStatus::Fresh);
if let Err(closed) = setting.gate(catalog) {
return Results {
status: ResultStatus::Off,
error: Some(closed),
..base
};
}
let answered = match catalog.kind {
CatalogKind::SkillsSh => {
skills_sh::search(parts.http, catalog, skills_sh::PING_QUERY)
.map(|_| ())
}
CatalogKind::McpRegistry => registry::ping(parts.http, catalog),
CatalogKind::GitTap => {
let got = tap_index(&parts, &Ok(()), true, catalog);
return with(base, got, |index| (index.entries, None));
}
};
match answered {
Ok(()) => {
let _ = cache::clear(&sx, &catalog.id);
Results {
fetched_at: Some(parts.env.now()),
age: Some(Duration::ZERO),
..base
}
}
Err(err) => Results {
status: ResultStatus::Error,
error: Some(err),
..base
},
}
})
})
.collect();
handles
.into_iter()
.zip(&catalogs)
.map(|(handle, catalog)| {
handle.join().unwrap_or_else(|_| {
let mut results = Results::new(catalog, ResultStatus::Error);
results.error = Some(CoreError::new(
ErrorCode::CatalogUnreachable,
"the refresh of this catalog stopped unexpectedly",
));
results
})
})
.collect()
}))
}
pub fn parse_ref(text: &str) -> Result<(String, String), CoreError> {
let bad = || {
CoreError::new(
ErrorCode::InvalidValue,
format!("{text:?} is not a catalog entry (catalog:<source>/<id>)"),
)
};
let rest = text.trim().strip_prefix(REF_PREFIX).ok_or_else(bad)?;
let (catalog, id) = rest.split_once('/').ok_or_else(bad)?;
if !is_catalog_id(catalog) || id.is_empty() || id.chars().any(char::is_control) {
return Err(bad());
}
Ok((catalog.to_owned(), id.to_owned()))
}
fn install_entry(
parts: &Parts<'_>,
read: Option<&dyn StoreRead>,
text: &str,
) -> Result<(Catalog, CatalogEntry, Option<tap::TapIndex>), CoreError> {
let (catalog_id, entry_id) = parse_ref(text)?;
let catalog = find(read, &catalog_id)?;
if !catalog.enabled {
return Err(CoreError::new(
ErrorCode::InvalidValue,
format!("the catalog {catalog_id:?} is turned off"),
));
}
online(parts.fs, parts.home)?.gate(&catalog)?;
match catalog.kind {
CatalogKind::SkillsSh => {
let entry = cache::entry_in_pages(
&parts.sx(),
&catalog.id,
&search_prefix(&catalog),
&entry_id,
)
.map(|hit| hit.data)
.or_else(|| skills_sh::entry_for(&catalog, &entry_id, None, None, None))
.ok_or_else(|| not_in(&catalog, &entry_id))?;
Ok((catalog, entry, None))
}
CatalogKind::McpRegistry => {
let entry = registry::entry(parts.http, &catalog, &entry_id, None);
entry.map(|entry| (catalog, entry, None))
}
CatalogKind::GitTap => {
let got = tap_index(parts, &Ok(()), false, &catalog);
let index = got
.data
.ok_or_else(|| got.error.unwrap_or_else(off_error))?;
let entry = index
.entries
.iter()
.find(|e| e.id == entry_id)
.cloned()
.ok_or_else(|| not_in(&catalog, &entry_id))?;
Ok((catalog, entry, Some(index)))
}
}
}
#[derive(Debug, Clone)]
pub enum McpRequest {
Registry(RegistryRequest),
Git(ResolveRequest),
}
#[derive(Debug, Clone)]
pub struct McpHandoff {
pub entry: CatalogEntry,
pub request: McpRequest,
}
impl McpHandoff {
pub fn trust_source(&self) -> Result<Option<SourceRef>, CoreError> {
match &self.request {
McpRequest::Registry(_) => Ok(None),
McpRequest::Git(request) => match &request.source {
Source::Git { url, .. } => SourceRef::from_git_url(url).map(Some),
Source::Local(folder) => SourceRef::from_local_path(folder).map(Some),
},
}
}
pub fn resolve(
&self,
store: Option<&Store<'_>>,
sx: &SkillsEnv<'_>,
) -> Result<Resolved, CoreError> {
match &self.request {
McpRequest::Registry(request) => mcp_source::resolve_registry(store, sx, request),
McpRequest::Git(request) => mcp_source::resolve(store, sx, request),
}
}
}
fn plugin_url(catalog: &Catalog, source: &PluginSource) -> Result<String, CoreError> {
match source.kind {
PluginSourceKind::Relative => Ok(catalog.url.clone()),
_ => source.url.clone().ok_or_else(|| {
CoreError::new(ErrorCode::InvalidValue, "this plugin names no repository")
}),
}
}
pub fn mcp_handoff(
cx: &CatalogEnv<'_>,
read: Option<&dyn StoreRead>,
text: &str,
version: Option<&str>,
) -> Result<McpHandoff, CoreError> {
let parts = Parts::of(cx);
let (catalog_id, entry_id) = parse_ref(text)?;
let catalog = find(read, &catalog_id)?;
if catalog.kind == CatalogKind::McpRegistry && version.is_some() {
if !catalog.enabled {
return Err(CoreError::new(
ErrorCode::InvalidValue,
format!("the catalog {catalog_id:?} is turned off"),
));
}
online(parts.fs, parts.home)?.gate(&catalog)?;
let entry = registry::entry(parts.http, &catalog, &entry_id, version)?;
return registry_handoff(&catalog, entry, true);
}
let (catalog, entry, _) = install_entry(&parts, read, text)?;
match catalog.kind {
CatalogKind::McpRegistry => registry_handoff(&catalog, entry, false),
_ => {
let source = match (&entry.kind, &entry.source) {
(EntryKind::Plugin, Some(source)) if entry.install_mcp => source.clone(),
(EntryKind::Skill, _) => {
return Err(CoreError::new(
ErrorCode::InvalidValue,
format!(
"{} is a skill: install it with `surject skills add {}`",
entry.id,
entry.reference()
),
));
}
_ => {
return Err(CoreError::new(
ErrorCode::InvalidValue,
entry.problem.clone().unwrap_or_else(|| {
format!("{} holds no MCP server Surjection imports", entry.id)
}),
));
}
};
let mut request = ResolveRequest::new(Source::Git {
url: plugin_url(&catalog, &source)?,
git_ref: None,
path: None,
});
request.git_ref = version
.map(str::to_owned)
.or_else(|| source.sha.clone())
.or_else(|| source.git_ref.clone());
request.path.clone_from(&source.path);
request.catalog = Some(format!("{}/{}", catalog.id, entry.id));
request.mcp_servers.clone_from(&entry.mcp_servers);
Ok(McpHandoff {
entry,
request: McpRequest::Git(request),
})
}
}
}
fn registry_handoff(
catalog: &Catalog,
entry: CatalogEntry,
pinned: bool,
) -> Result<McpHandoff, CoreError> {
if !entry.install_mcp {
return Err(CoreError::new(
ErrorCode::InvalidValue,
entry
.problem
.clone()
.unwrap_or_else(|| "this entry cannot be installed".to_owned()),
));
}
let server_json = entry.server_json.clone().ok_or_else(|| {
CoreError::new(ErrorCode::InvalidValue, "the entry carries no server.json")
})?;
let version = entry
.version
.clone()
.ok_or_else(|| CoreError::new(ErrorCode::InvalidValue, "the entry names no version"))?;
Ok(McpHandoff {
request: McpRequest::Registry(RegistryRequest {
catalog: format!("{}/{}", catalog.id, entry.id),
registry: catalog.url.clone(),
name: entry.id.clone(),
version,
pinned,
server_json,
}),
entry,
})
}
pub fn skill_request(
cx: &CatalogEnv<'_>,
read: Option<&dyn StoreRead>,
text: &str,
git_ref: Option<&str>,
path: Option<&str>,
) -> Result<(InstallRequest, CatalogEntry), CoreError> {
let parts = Parts::of(cx);
let (catalog, entry, index) = install_entry(&parts, read, text)?;
let catalog_ref = Some(format!("{}/{}", catalog.id, entry.id));
if catalog.kind == CatalogKind::SkillsSh {
if !entry.install_skill {
return Err(CoreError::new(
ErrorCode::InvalidValue,
entry
.problem
.clone()
.unwrap_or_else(|| skills_sh::WEBSITE_PROBLEM.to_owned()),
));
}
let mut request = skills_sh_request(&skills_sh::page_url(&catalog, &entry.id))
.ok_or_else(|| not_in(&catalog, &entry.id))??;
request.git_ref = git_ref.map(str::to_owned);
request.path = path.map(str::to_owned);
request.catalog = catalog_ref;
return Ok((request, entry));
}
let pin = |entry: &CatalogEntry| -> Option<String> {
if entry.pinned {
entry.commit.clone()
} else {
entry.git_ref.clone()
}
};
let (url, entry_ref, entry_path) = match entry.kind {
EntryKind::Server => {
return Err(CoreError::new(
ErrorCode::InvalidValue,
format!(
"{} is an MCP server: add it with `surject server add --from {}`",
entry.id,
entry.reference()
),
));
}
EntryKind::Skill => (catalog.url.clone(), pin(&entry), entry.path.clone()),
EntryKind::Plugin => {
let source = entry
.source
.clone()
.filter(|_| entry.install_skill)
.ok_or_else(|| {
CoreError::new(
ErrorCode::InvalidValue,
entry.problem.clone().unwrap_or_else(|| {
format!("{} holds no skill Surjection installs", entry.id)
}),
)
})?;
let folder = match (source.kind, entry.skills.as_slice()) {
(PluginSourceKind::Relative, _) if path.is_some() => None,
(PluginSourceKind::Relative, [one]) => index
.as_ref()
.and_then(|index| index.entries.iter().find(|e| &e.id == one))
.and_then(|skill| skill.path.clone()),
(PluginSourceKind::Relative, many) => {
return Err(CoreError::new(
ErrorCode::InvalidValue,
format!(
"{} holds {} skills; install one of them: {}",
entry.id,
many.len(),
many.iter()
.map(|id| format!("{REF_PREFIX}{}/{id}", catalog.id))
.collect::<Vec<_>>()
.join(", ")
),
));
}
_ => source.path.clone(),
};
(
plugin_url(&catalog, &source)?,
source.sha.clone().or_else(|| source.git_ref.clone()),
folder,
)
}
};
let mut request = InstallRequest::new(Source::Git {
url,
git_ref: None,
path: None,
});
request.git_ref = git_ref.map(str::to_owned).or(entry_ref);
request.path = path.map(str::to_owned).or(entry_path);
request.catalog = catalog_ref;
Ok((request, entry))
}
pub fn skills_sh_request(text: &str) -> Option<Result<InstallRequest, CoreError>> {
let url = url::Url::parse(text.trim()).ok()?;
if !matches!(url.host_str(), Some("skills.sh" | "www.skills.sh")) {
return None;
}
let parts: Vec<&str> = url
.path_segments()
.map(|segments| segments.filter(|s| !s.is_empty()).collect())
.unwrap_or_default();
let valid = |part: &str| {
!part.is_empty()
&& part != "."
&& part != ".."
&& part
.chars()
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-'))
};
Some(match parts.as_slice() {
["site", domain, ..] => Err(CoreError::new(
ErrorCode::InvalidValue,
format!(
"skills.sh lists this skill from {domain}, not from a GitHub repository; Surjection \
installs skills.sh entries from GitHub repositories only"
),
)),
[owner, repo, rest @ ..]
if valid(owner) && valid(repo) && rest.iter().all(|p| valid(p)) =>
{
let mut request = InstallRequest::new(Source::Git {
url: format!("https://github.com/{owner}/{repo}"),
git_ref: None,
path: None,
});
request.skill = rest.first().map(|skill| (*skill).to_owned());
Ok(request)
}
_ => Err(CoreError::new(
ErrorCode::InvalidValue,
"this skills.sh address names no skill (https://skills.sh/<owner>/<repo>/<skill>)",
)),
})
}
pub(crate) fn registry_latest(
read: &dyn StoreRead,
sx: &SkillsEnv<'_>,
source: &ServerSource,
) -> Result<RegistryRequest, CoreError> {
require_online(sx.fs, sx.home)?;
let missing = |what: &str| {
CoreError::new(
ErrorCode::MissingField,
format!("the registry source has no {what}"),
)
};
let reference = source
.catalog
.as_deref()
.ok_or_else(|| missing("catalog"))?;
let (catalog_id, name) = reference
.split_once('/')
.ok_or_else(|| missing("catalog entry"))?;
let url = source.url.clone().ok_or_else(|| missing("url"))?;
if let Some(problem) = registry_url_problem(&url) {
return Err(CoreError::new(ErrorCode::InvalidValue, problem));
}
let catalog = find(Some(read), catalog_id)
.ok()
.filter(|catalog| catalog.kind == CatalogKind::McpRegistry && catalog.url == url)
.ok_or_else(|| {
CoreError::new(
ErrorCode::NotFound,
format!(
"the catalog {catalog_id:?} is no longer in your catalogs at this address: \
add it again to check for updates"
),
)
})?;
if !catalog.enabled {
return Err(CoreError::new(
ErrorCode::InvalidValue,
format!("the catalog {catalog_id:?} is turned off: turn it on to check for updates"),
));
}
online(sx.fs, sx.home)?.gate(&catalog)?;
let http = UreqHttp::new(sx.env, HttpLimits::default());
let entry = registry::entry(&http, &catalog, name, None)?;
match registry_handoff(&catalog, entry, false)?.request {
McpRequest::Registry(request) => Ok(request),
McpRequest::Git(_) => unreachable!("a registry entry"),
}
}