From c3262af64cdd7551f8bfe21a3e0a7cf95119c9f3 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Sat, 10 Oct 2026 20:03:33 +0000 Subject: [PATCH 1/2] feat(extensions): install skills and plugins from Anthropic-format marketplaces Admins add marketplaces (Anthropic's are suggested, any repository with .claude-plugin/marketplace.json works); people who write extensions browse, preview and install plugins from them, or import a plugin or skill from a GitHub/GitLab link or a .zip. Skills keep their text files, read on demand with use_skill; commands become skills and agents prompt tools. Claude Code hooks and plugin MCP servers are left out and named in the preview. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01CsyA2Kuhsnhzz2HjkD3zED --- docs/assistant.md | 20 + fastapi_backend/app/api/v1/router.py | 2 + .../app/api/v1/routes/extensions.py | 27 +- .../app/api/v1/routes/marketplaces.py | 80 ++ fastapi_backend/app/domain/activity.py | 1 + fastapi_backend/app/domain/ai_tools.py | 9 +- fastapi_backend/app/domain/extensions.py | 45 +- fastapi_backend/app/domain/marketplace.py | 709 +++++++++++++++ fastapi_backend/app/domain/store.py | 1 + fastapi_backend/app/schemas/extensions.py | 66 +- .../tests/api/test_marketplaces.py | 259 ++++++ .../__tests__/marketplace-ui.test.tsx | 129 +++ .../app/(app)/extensions/marketplace/page.tsx | 10 + nextjs-frontend/app/openapi-client/index.ts | 55 ++ nextjs-frontend/app/openapi-client/sdk.gen.ts | 179 ++++ .../app/openapi-client/types.gen.ts | 525 ++++++++++- .../components/extensions/extensions-page.tsx | 15 +- .../extensions/marketplace-page.tsx | 455 ++++++++++ nextjs-frontend/openapi.json | 841 +++++++++++++++++- 19 files changed, 3396 insertions(+), 32 deletions(-) create mode 100644 fastapi_backend/app/api/v1/routes/marketplaces.py create mode 100644 fastapi_backend/app/domain/marketplace.py create mode 100644 fastapi_backend/tests/api/test_marketplaces.py create mode 100644 nextjs-frontend/__tests__/marketplace-ui.test.tsx create mode 100644 nextjs-frontend/app/(app)/extensions/marketplace/page.tsx create mode 100644 nextjs-frontend/components/extensions/marketplace-page.tsx diff --git a/docs/assistant.md b/docs/assistant.md index 1ca8ebef2..1e221afe0 100644 --- a/docs/assistant.md +++ b/docs/assistant.md @@ -172,6 +172,26 @@ when: someone asks for a recap of a meeting Find the meeting, read it, and answer with three bullets: decisions, owners, dates. ``` +### Skills and plugins from marketplaces + +Lens reads Anthropic's open plugin and skill formats as they are, so what's made for Claude installs here unchanged: + +- **Marketplaces** (`/api/v1/marketplaces`): a repository with `.claude-plugin/marketplace.json`. Admins add them, + which is the approval: Anthropic's own (`anthropics/skills`, `anthropics/claude-plugins-official`) are suggested, + and any other is added by its address (`owner/repo`, a GitHub or GitLab link, or a link to its marketplace.json), + which is how a team runs a custom one. Everyone who can edit browses them, previews a plugin (what it adds and what + Lens leaves out) and installs it. +- **Links and files** (`POST /api/v1/extensions/import`, `/import/archive`): a plugin or a skill from a GitHub or + GitLab repository or folder, or a `.zip`, `.skill` or `.tar.gz`. + +An installed plugin is one Lens plugin, yours and private until you share it; installing it again saves its new +version. A skill (a folder with SKILL.md) is a skill, and the assistant reads its text files (references, examples, +scripts) when the instructions point at them; nothing in them runs. A command (`commands/*.md`) is a skill the +assistant follows when someone asks for it, and an agent (`agents/*.md`) is a prompt tool with the agent's instructions +as its system prompt. Claude Code's hooks run commands on the computer, so they're left out, and so are a plugin's MCP +servers for now. Repositories are read from GitHub or GitLab (or the archive link) through the same guard as web +tools: public addresses only. + `ai.extensions: false` turns every extension off. ## Setting up and running the server by chat diff --git a/fastapi_backend/app/api/v1/router.py b/fastapi_backend/app/api/v1/router.py index d6ec4e60b..f6186a5e2 100644 --- a/fastapi_backend/app/api/v1/router.py +++ b/fastapi_backend/app/api/v1/router.py @@ -28,6 +28,7 @@ import_hooks, imports, jobs, + marketplaces, metadata, namespaces, notebook, @@ -106,6 +107,7 @@ workflows, custom_nodes, extensions, + marketplaces, chats, collections, batches, diff --git a/fastapi_backend/app/api/v1/routes/extensions.py b/fastapi_backend/app/api/v1/routes/extensions.py index d86a715af..17ad73fad 100644 --- a/fastapi_backend/app/api/v1/routes/extensions.py +++ b/fastapi_backend/app/api/v1/routes/extensions.py @@ -9,12 +9,16 @@ from fastapi import APIRouter, HTTPException, Request from app.api.deps import Acl, Cfg, CurrentUser, Db, Principal, Writer, domain_errors -from app.domain import ai_tools, auth, extensions +from app.domain import ai_tools, auth, extensions, marketplace from app.schemas.common import Created, Ok from app.schemas.extensions import ( Extension, ExtensionCreate, ExtensionDetail, + ExtensionImportArchive, + ExtensionImported, + ExtensionImportLink, + ExtensionImportPreview, ExtensionTest, ExtensionTestResult, ExtensionUpdate, @@ -68,6 +72,27 @@ def create_extension(body: ExtensionCreate, user: Writer, db: Db) -> Created: return Created(id=eid) +@router.post("/import") +def import_link(body: ExtensionImportLink, user: Writer, db: Db, cfg: Cfg) -> ExtensionImportPreview | ExtensionImported: + """A plugin or skill in Anthropic's format from a link: a GitHub or GitLab repository or folder, or a .zip. With + `preview`, what it would add and leave out; else installed as your own (importing it again: a new version).""" + with _errors(): + out = marketplace.import_link(db, cfg, me(user), body.url, dry=body.preview) + if not body.preview: + auth.audit(db, user.as_audit(), "extension.import", f"extension:{out['id']}", {"url": body.url, "version": out["version"]}) + return ExtensionImportPreview(**out) if body.preview else ExtensionImported(**out) + + +@router.post("/import/archive") +def import_archive(body: ExtensionImportArchive, user: Writer, db: Db) -> ExtensionImportPreview | ExtensionImported: + """A plugin or skill from a .zip, .skill or .tar.gz you send (as base64).""" + with _errors(): + out = marketplace.import_archive(db, me(user), body.filename, body.data, dry=body.preview) + if not body.preview: + auth.audit(db, user.as_audit(), "extension.import", f"extension:{out['id']}", {"file": body.filename, "version": out["version"]}) + return ExtensionImportPreview(**out) if body.preview else ExtensionImported(**out) + + @router.get("/{eid}") def get_extension(eid: int, user: CurrentUser, db: Db, version: int | None = None) -> ExtensionDetail: """One version (default: the current one), as a manifest too, and the list of versions.""" diff --git a/fastapi_backend/app/api/v1/routes/marketplaces.py b/fastapi_backend/app/api/v1/routes/marketplaces.py new file mode 100644 index 000000000..bb112b19a --- /dev/null +++ b/fastapi_backend/app/api/v1/routes/marketplaces.py @@ -0,0 +1,80 @@ +"""Marketplaces: where plugins and skills in Anthropic's format are installed from. Admins add them; people who write +extensions install from them.""" + +from __future__ import annotations + +from fastapi import APIRouter + +from app.api.deps import AdminWriter, Cfg, CurrentUser, Db, Writer +from app.api.v1.routes.extensions import _errors, me +from app.domain import auth, marketplace +from app.schemas.common import Created, Ok +from app.schemas.extensions import ( + ExtensionImported, + ExtensionImportPreview, + Marketplace, + MarketplaceCreate, + MarketplacePlugin, + MarketplaceSuggestion, +) + +router = APIRouter(prefix="/marketplaces", tags=["extensions"]) + + +@router.get("") +def list_marketplaces(user: CurrentUser, db: Db) -> list[Marketplace]: + return marketplace.listing(db) + + +@router.get("/suggested") +def suggested_marketplaces(user: CurrentUser, db: Db) -> list[MarketplaceSuggestion]: + """Anthropic's marketplaces, when they aren't added yet.""" + return marketplace.suggested(db) + + +@router.post("") +def add_marketplace(body: MarketplaceCreate, user: AdminWriter, db: Db, cfg: Cfg) -> Created: + """Add a marketplace by its address; its list of plugins is read now.""" + with _errors(): + mid = marketplace.add(db, cfg, me(user), body.source, body.title) + auth.audit(db, user.as_audit(), "marketplace.add", f"marketplace:{mid}", {"source": body.source}) + return Created(id=mid) + + +@router.post("/{mid}/refresh") +def refresh_marketplace(mid: int, user: AdminWriter, db: Db, cfg: Cfg) -> Ok: + """Read its list of plugins again.""" + with _errors(): + marketplace.refresh(db, cfg, me(user), mid) + return Ok() + + +@router.delete("/{mid}") +def delete_marketplace(mid: int, user: AdminWriter, db: Db) -> Ok: + """Takes it away; what was installed from it stays.""" + with _errors(): + marketplace.remove(db, me(user), mid) + auth.audit(db, user.as_audit(), "marketplace.delete", f"marketplace:{mid}") + return Ok() + + +@router.get("/{mid}/plugins") +def list_marketplace_plugins(mid: int, user: CurrentUser, db: Db) -> list[MarketplacePlugin]: + with _errors(): + return marketplace.plugins(db, me(user), mid) + + +@router.post("/{mid}/plugins/{name}/preview") +def preview_marketplace_plugin(mid: int, name: str, user: Writer, db: Db, cfg: Cfg) -> ExtensionImportPreview: + """What installing it would add to the assistant, and what Lens leaves out.""" + with _errors(): + return marketplace.preview(db, cfg, me(user), mid, name) + + +@router.post("/{mid}/plugins/{name}/install") +def install_marketplace_plugin(mid: int, name: str, user: Writer, db: Db, cfg: Cfg) -> ExtensionImported: + """Install it as your own plugin, private until you share it; installing it again brings its new version.""" + with _errors(): + eid, n = marketplace.install(db, cfg, me(user), mid, name) + auth.audit(db, user.as_audit(), "extension.import", f"extension:{eid}", {"marketplace": mid, "plugin": name, "version": n}) + return ExtensionImported(id=eid, version=n) diff --git a/fastapi_backend/app/domain/activity.py b/fastapi_backend/app/domain/activity.py index 4f4d0e2bb..9e44e0a59 100644 --- a/fastapi_backend/app/domain/activity.py +++ b/fastapi_backend/app/domain/activity.py @@ -51,6 +51,7 @@ "spaces": "space", "collections": "collection", "extensions": "extension", + "marketplaces": "marketplace", "sources": "watch_path", "watches": "watch_path", "sensors": "sensor", diff --git a/fastapi_backend/app/domain/ai_tools.py b/fastapi_backend/app/domain/ai_tools.py index c41b2f824..60609f3a6 100644 --- a/fastapi_backend/app/domain/ai_tools.py +++ b/fastapi_backend/app/domain/ai_tools.py @@ -483,13 +483,20 @@ def run(**args): return run - def t_use_skill(self, name): + def t_use_skill(self, name, file=None): s = (self.ext.skills if self.ext else {}).get(name) if not s: raise ValueError(f"no skill {name}") + files = s["spec"].get("files") or {} + if file: + if file not in files: + raise ValueError(f"the skill {name} has no file {file}; it has: {', '.join(files) or 'none'}") + return {"skill": name, "file": file, "text": files[file]}, f"Read {file} from the skill {name}" out = {"skill": name, "instructions": s["spec"]["instructions"]} if s["spec"].get("tools"): out["tools"] = s["spec"]["tools"] + if files: + out["files"] = list(files) return out, f"Read the skill {name}" # ---- extensions, from chat ---- diff --git a/fastapi_backend/app/domain/extensions.py b/fastapi_backend/app/domain/extensions.py index 99814c910..2303dc844 100644 --- a/fastapi_backend/app/domain/extensions.py +++ b/fastapi_backend/app/domain/extensions.py @@ -53,6 +53,10 @@ HOOK_VARS = {"message": {"said"}, "before_tool": {"said", "tool"}, "after_tool": {"said", "tool", "result"}, "answer": {"said", "answer"}} VAR_RX = re.compile(r"\{\{\s*([a-z][a-z0-9_]*)\s*\}\}") MAX_TEXT = 20000 +MAX_SKILL = 200_000 # a skill's instructions: Anthropic's own run to 100,000 characters +MAX_FILES = 100 # files a skill carries besides its instructions (references, examples, scripts to read) +MAX_FILES_CHARS = 500_000 +FILE_RX = re.compile(r"^(?!/)(?!.*(?:^|/)\.\.(?:/|$))[^\x00-\x1f\\]{1,200}$") MAX_PARAMS = 20 MAX_ITEMS = 50 MAX_REPLY = 200_000 # the most of a web reply that is read @@ -180,20 +184,36 @@ def _check_tool(spec, me, db=None): def _check_skill(spec, me, db=None): - if set(spec) - {"when", "instructions", "tools"}: - raise ValueError("a skill is {when, instructions, tools}") + if set(spec) - {"when", "instructions", "tools", "files"}: + raise ValueError("a skill is {when, instructions, tools, files}") tools = spec.get("tools") or [] if not isinstance(tools, list) or not all(isinstance(t, str) for t in tools): raise ValueError("a skill's tools are a list of tool names") return store.clean( { - "when": _text(spec.get("when"), "when to use the skill", 500, required=True), - "instructions": _text(spec.get("instructions"), "the skill's instructions", MAX_TEXT, required=True), + "when": _text(spec.get("when"), "when to use the skill", 1024, required=True), + "instructions": _text(spec.get("instructions"), "the skill's instructions", MAX_SKILL, required=True), "tools": tools[:30] or None, + "files": _check_files(spec.get("files")), } ) +def _check_files(files): + """A skill's other files, {path: text}, as a skill folder has them next to its SKILL.md: the assistant reads one + when the instructions point at it (use_skill with the file).""" + if not files: + return None + if not isinstance(files, dict) or len(files) > MAX_FILES: + raise ValueError(f"a skill's files are at most {MAX_FILES}, each a path and its text") + for path, text in files.items(): + if not isinstance(path, str) or not FILE_RX.match(path) or not isinstance(text, str): + raise ValueError(f"a skill's file is a path inside its folder and the file's text: {str(path)[:80]}") + if sum(len(t) for t in files.values()) > MAX_FILES_CHARS: + raise ValueError(f"a skill's files come to at most {MAX_FILES_CHARS} characters") + return dict(sorted(files.items())) + + def _check_hook(spec, me, db=None): if set(spec) - {"event", "match", "action"}: raise ValueError("a hook is {event, match, action}") @@ -291,7 +311,7 @@ def check_manifest(m, me, db=None): "name": name, "kind": head["kind"], "title": _text(head["title"], "the title", 80), - "description": _text(head["description"], "the description", 1000, required=head["kind"] != "hook"), + "description": _text(head["description"], "the description", 1024, required=head["kind"] != "hook"), "visibility": head["visibility"], "namespaces": head["namespaces"], "spec": check_spec(head["kind"], spec, me, db), @@ -409,8 +429,9 @@ def _check_share(db, me, visibility, namespaces): return vis, sorted(set(sids)) -def create(db, me, manifest, enabled=True, origin="code"): - """A new extension from a manifest; its id. `origin` says how it was made: code, canvas or chat.""" +def create(db, me, manifest, enabled=True, origin="code", source=None): + """A new extension from a manifest; its id. `origin` says how it was made: code, canvas or chat (or import, from a + marketplace or a link, when `source` says where from: marketplace.py).""" m = check_manifest(manifest, me, db) _check_names(db, m["kind"], m["name"], m["spec"]) vis, sids = _check_share(db, me, m.get("visibility"), m.get("namespaces")) @@ -427,6 +448,7 @@ def create(db, me, manifest, enabled=True, origin="code"): "visibility": vis, "namespaces": sids, "enabled": bool(enabled), + "source": source, "owner": me["id"], "owner_email": me["email"], "current": 1, @@ -460,7 +482,7 @@ def _save(db, me, eid, n, spec, notes, origin): def _row(db, eid): d = db.one( "SELECT record::id(id) AS id, name, kind, title, description, visibility, namespaces, enabled, owner, owner_email, " - "current, created_at, updated_at, deleted_at FROM $r", + "current, source, created_at, updated_at, deleted_at FROM $r", r=R("extension", int(eid)), ) if not d: @@ -514,7 +536,7 @@ def update(db, me, eid, **meta): if "title" in meta: out["title"] = _text(meta["title"], "the title", 80) if "description" in meta and meta["description"] is not None: - out["description"] = _text(meta["description"], "the description", 1000) + out["description"] = _text(meta["description"], "the description", 1024) if meta.get("enabled") is not None: out["enabled"] = bool(meta["enabled"]) if meta.get("visibility") is not None or meta.get("namespaces") is not None: @@ -637,12 +659,15 @@ def tool_specs(self, can_change=True): desc = (t["description"] or name) + (" Needs the person's approval." if t["spec"]["effect"] == "change" else "") out.append((name, desc, props, req, t["spec"]["effect"] == "change")) if self.skills: + props = {"name": {"type": "string", "enum": list(self.skills)}} + if any(s["spec"].get("files") for s in self.skills.values()): + props["file"] = {"type": "string", "description": "one of the skill's files, when its instructions point at one"} out.append( ( "use_skill", "Read a skill's instructions before doing what it's for. Skills: " + "; ".join(f"{n} ({s['spec']['when']})" for n, s in self.skills.items()), - {"name": {"type": "string", "enum": list(self.skills)}}, + props, ["name"], False, ) diff --git a/fastapi_backend/app/domain/marketplace.py b/fastapi_backend/app/domain/marketplace.py new file mode 100644 index 000000000..4661e0049 --- /dev/null +++ b/fastapi_backend/app/domain/marketplace.py @@ -0,0 +1,709 @@ +"""Marketplaces: plugins and skills people made for other assistants, installed as Lens extensions (extensions.py). + +Lens reads Anthropic's open formats as they are, so a marketplace made for Claude works here unchanged: + +- A marketplace is a repository with `.claude-plugin/marketplace.json`: its name and a list of plugins, each with a + source (a folder in the same repository, a GitHub repository, a git link, or a folder of one: `github`, `url`, + `git-subdir`). Anthropic's own are suggested (ANTHROPIC); any other is added by its address, which is how a team + runs a custom one. +- A plugin is a folder: `.claude-plugin/plugin.json` (optional), `skills//SKILL.md`, `commands/*.md`, + `agents/*.md`, `hooks/hooks.json` and `.mcp.json`. A marketplace entry with `strict: false` describes the plugin + itself, and its `skills`, `commands` and `agents` name folders and files in the source. +- A skill is a folder with SKILL.md (frontmatter `name` and `description`, then the instructions) and any other files + the instructions point at. + +What becomes what: a skill is a skill, with its text files (references, examples, scripts to read; nothing runs); a +command is a skill the assistant follows when someone asks for it; an agent is a prompt tool with the agent's +instructions as its system prompt. Claude Code's hooks run shell commands on the computer, so they're left out, and so +are MCP servers for now; `preview` lists what's left out and why. A plugin becomes one Lens plugin, owned by the person +who installed it and private until they share it, and installing it again brings the new version. + +Only admins add marketplaces: that's the approval. People who can write extensions install from the ones added, and +import a plugin or skill from a link or a .zip themselves, as they could write it by hand. Repositories are read from +GitHub or GitLab, or from a .zip or .tar.gz link, through netguard (public addresses only); nothing from them runs. +""" + +from __future__ import annotations + +import base64 +import io +import json +import posixpath +import re +import tarfile +import tempfile +import threading +import time +import urllib.error +import urllib.parse +import urllib.request +import zipfile + +import yaml + +from . import activity, extensions, store + +R = store.R +ANTHROPIC = ( + {"name": "anthropic-agent-skills", "title": "Anthropic skills", "source": "anthropics/skills"}, + {"name": "claude-plugins-official", "title": "Anthropic plugins", "source": "anthropics/claude-plugins-official"}, +) +MANIFEST = ".claude-plugin/marketplace.json" +PLUGIN_JSON = ".claude-plugin/plugin.json" +MAX_INDEX = 5 * 2**20 # a marketplace.json +MAX_ARCHIVE = 200 * 2**20 # a repository's archive, downloaded +MAX_UPLOAD = 20 * 2**20 # a .zip sent from the web app +MAX_FILE = 1 * 2**20 # a file read out of one +MAX_READ = 30 * 2**20 # all the files read out of one +MAX_ENTRIES = 20000 +TEXT = { + ".md", + ".markdown", + ".txt", + ".rst", + ".py", + ".js", + ".mjs", + ".ts", + ".sh", + ".json", + ".yaml", + ".yml", + ".toml", + ".csv", + ".html", + ".css", + ".sql", +} +CACHE_SECONDS = 600 +UA = "Lens marketplace" +FIELDS = "record::id(id) AS id, name, title, description, metadata, source, added_by, created_at, fetched_at" +_cache: dict[tuple, tuple[float, dict]] = {} +_lock = threading.Lock() + + +# ---------- where things are ---------- +def parse_source(text): + """Where a marketplace, plugin or skill is, from what people paste: `owner/repo` (GitHub, `@ref` for a branch or + tag), a GitHub or GitLab link (to the repository or a folder in it), a link to a marketplace.json, or a link to a + .zip or .tar.gz. A dict {host, repo, ref, path} or {host: url|archive, url}; ValueError otherwise.""" + t = (text or "").strip() + m = re.fullmatch(r"([A-Za-z0-9][\w.-]*)/([\w.-]+?)(?:\.git)?(?:@([\w./-]+))?", t) + if m: + return store.clean({"host": "github", "repo": f"{m[1]}/{m[2]}", "ref": m[3]}) + u = urllib.parse.urlsplit(t) + if u.scheme not in ("http", "https") or not u.hostname: + raise ValueError("give a GitHub repository (owner/repo) or a link that starts with https://") + path = u.path.rstrip("/") + if u.hostname in ("github.com", "www.github.com"): + m = re.fullmatch(r"/([\w.-]+)/([\w.-]+?)(?:\.git)?(?:/(?:tree|blob)/([^/]+)(?:/(.*))?)?", path) + if m: + sub = m[4] + if sub and sub.endswith((".json", ".md")): # a file: its folder (a marketplace.json is two down) + sub = posixpath.dirname(sub) + sub = posixpath.dirname(sub) if sub.endswith(".claude-plugin") else sub + return store.clean({"host": "github", "repo": f"{m[1]}/{m[2]}", "ref": m[3], "path": sub or None}) + if u.hostname == "raw.githubusercontent.com": + m = re.fullmatch(r"/([\w.-]+)/([\w.-]+)/([^/]+)/(.*)", path) + if m and m[4].endswith(MANIFEST): + return store.clean({"host": "github", "repo": f"{m[1]}/{m[2]}", "ref": m[3], "path": m[4][: -len(MANIFEST)].strip("/") or None}) + if u.hostname == "gitlab.com": + m = re.fullmatch(r"/(.+?)(?:\.git)?(?:/-/(?:tree|blob)/([^/]+)(?:/(.*))?)?", path) + if m and "/" in m[1]: + return store.clean({"host": "gitlab", "repo": m[1], "ref": m[2], "path": m[3] or None}) + if path.endswith((".zip", ".tar.gz", ".tgz", ".skill")): + return {"host": "archive", "url": t} + if path.endswith(".json"): + return {"host": "url", "url": t} + raise ValueError("Lens reads GitHub and GitLab repositories, links to a marketplace.json, and .zip or .tar.gz links") + + +def show_source(src): + """A source as people would write it.""" + if src.get("host") in ("url", "archive"): + return src["url"] + base = f"https://{'github.com' if src['host'] == 'github' else 'gitlab.com'}/{src['repo']}" + if src.get("path") or src.get("ref"): + base += f"{'/-' if src['host'] == 'gitlab' else ''}/tree/{src.get('ref') or 'HEAD'}" + base += f"/{src['path']}" if src.get("path") else "" + return base + + +def plugin_source(entry, market): + """Where a marketplace's plugin is: its `source`, read against the marketplace's own place.""" + s = entry.get("source") + if isinstance(s, str): + if not market or market.get("host") not in ("github", "gitlab"): + raise ValueError("this plugin is a folder in its marketplace, which Lens can only read from GitHub or GitLab") + root = (market.get("metadata") or {}).get("pluginRoot") or "" + rel = s if s.startswith("./") or s.startswith("../") or not root else posixpath.join(root, s) + path = posixpath.normpath(posixpath.join(market.get("path") or "", rel)) + if path.startswith(".."): + raise ValueError("the plugin's folder is outside its marketplace") + return store.clean( + {"host": market["host"], "repo": market["repo"], "ref": market.get("ref"), "path": None if path == "." else path} + ) + if not isinstance(s, dict): + raise ValueError("the plugin has no source") + kind = s.get("source") + ref = s.get("sha") or s.get("ref") + if kind == "github" and isinstance(s.get("repo"), str): + return store.clean({**parse_source(s["repo"]), "ref": ref, "path": s.get("path")}) + if kind in ("url", "git-subdir", "git") and isinstance(s.get("url"), str): + src = parse_source(s["url"]) + if src["host"] not in ("github", "gitlab"): + raise ValueError("Lens reads plugins from GitHub and GitLab; this one is elsewhere") + return store.clean({**src, "ref": ref or src.get("ref"), "path": s.get("path") or src.get("path")}) + raise ValueError(f"Lens can't read a plugin from a {kind or 'source like this'} source yet") + + +def _archive_url(src): + ref = urllib.parse.quote(src.get("ref") or "HEAD", safe="") + if src["host"] == "github": + return f"https://codeload.github.com/{src['repo']}/tar.gz/{ref}" + if src["host"] == "gitlab": + return f"https://gitlab.com/{src['repo']}/-/archive/{ref}/archive.tar.gz" + return src["url"] + + +def _raw_url(src, path): + ref = urllib.parse.quote(src.get("ref") or "HEAD", safe="") + path = urllib.parse.quote(posixpath.join(src.get("path") or "", path)) + if src["host"] == "github": + return f"https://raw.githubusercontent.com/{src['repo']}/{ref}/{path}" + return f"https://gitlab.com/{src['repo']}/-/raw/{ref}/{path}" + + +# ---------- fetching ---------- +def _fetch(cfg, url, out, limit): + """Download `url` into the open file `out` through netguard, at most `limit` bytes; ValueError saying why not.""" + from . import netguard, webcapture + + webcapture.check_url(cfg, url) + req = urllib.request.Request(url, headers={"User-Agent": UA}) + host = urllib.parse.urlsplit(url).hostname + with ( + activity.call("marketplace.fetch", cfg, detail={"host": host}), + netguard.Guard(forward=True, networks=webcapture.networks(cfg), max_bytes=limit + 65536) as guard, + ): + try: + with netguard.opener(guard).open(req, timeout=60) as r: + n = 0 + while chunk := r.read(65536): + n += len(chunk) + if n > limit: + raise ValueError(f"{host} sent more than {limit // 2**20} MB") + out.write(chunk) + except urllib.error.HTTPError as e: + raise ValueError( + f"{host} answered {e.code} {e.reason}" + (": is the address right, and public?" if e.code == 404 else "") + ) from None + except (urllib.error.URLError, OSError) as e: + refused = f"; refused: {guard.refused[0]}" if guard.refused else "" + raise ValueError(f"couldn't reach {host}: {getattr(e, 'reason', e)}{refused}") from None + + +def _get(cfg, url, limit): + buf = io.BytesIO() + _fetch(cfg, url, buf, limit) + return buf.getvalue() + + +def _members(fileobj, name): + """(path, size, read) for each regular file in a .tar.gz or .zip, whichever it is.""" + head = fileobj.read(4) + fileobj.seek(0) + if head.startswith(b"PK"): + z = zipfile.ZipFile(fileobj) + for i in z.infolist()[:MAX_ENTRIES]: + if not i.is_dir() and not (i.external_attr >> 16) & 0o170000 == 0o120000: # no symbolic links + yield i.filename, i.file_size, lambda i=i: z.read(i) + return + try: + t = tarfile.open(fileobj=fileobj, mode="r:*") + except tarfile.TarError: + raise ValueError(f"{name} isn't a .zip or .tar.gz") from None + for n, i in enumerate(t): + if n >= MAX_ENTRIES: + break + if i.isfile(): + yield i.name, i.size, lambda i=i: t.extractfile(i).read() + + +def _unpack(fileobj, name, sub=None, strip=None): + """The files of an archive under folder `sub`, {path relative to it: bytes}, where only text is read (other files + are None). `strip` drops the one folder a repository's archive puts everything in (None: drop it when everything + is in one folder).""" + entries = [] + for path, size, read in _members(fileobj, name): + p = posixpath.normpath(path.lstrip("/")) + if p.startswith("..") or p == ".": + continue + entries.append((p, size, read)) + if strip is None: + tops = {p.split("/", 1)[0] for p, _, _ in entries} + strip = len(tops) == 1 and all("/" in p for p, _, _ in entries) + out, total = {}, 0 + prefix = (sub.strip("/") + "/") if sub else "" + for p, size, read in entries: + if strip: + p = p.split("/", 1)[1] if "/" in p else "" + if not p or not p.startswith(prefix): + continue + rel = p[len(prefix) :] + if "__MACOSX/" in f"/{rel}" or rel.endswith(".DS_Store") or "/.git/" in f"/{rel}": + continue + if size > MAX_FILE or posixpath.splitext(rel)[1].lower() not in TEXT: + out[rel] = None + continue + total += size + if total > MAX_READ: + raise ValueError(f"{name} has more than {MAX_READ // 2**20} MB of files in that folder") + out[rel] = read() + if not out: + raise ValueError(f"{name} has nothing in {sub}" if sub else f"{name} is empty") + return out + + +def files(cfg, src): + """The files of a plugin or skill folder, {path: bytes}: from the repository's (or the link's) archive, kept a + few minutes so a preview and an install download it once.""" + key = (_archive_url(src), src.get("path")) + with _lock: + hit = _cache.get(key) + if hit and time.monotonic() - hit[0] < CACHE_SECONDS: + return hit[1] + with tempfile.TemporaryFile() as tmp: + _fetch(cfg, key[0], tmp, MAX_ARCHIVE) + tmp.seek(0) + got = _unpack(tmp, show_source(src), src.get("path"), strip=True if src["host"] != "archive" else None) + with _lock: + for k in [k for k, v in _cache.items() if time.monotonic() - v[0] >= CACHE_SECONDS]: + del _cache[k] + _cache[key] = (time.monotonic(), got) + return got + + +# ---------- reading Anthropic's formats ---------- +def slug(name): + """A name as Lens names tools and skills: lowercase letters, digits and _, starting with a letter.""" + s = re.sub(r"[^a-z0-9]+", "_", str(name or "").lower()).strip("_") + if s and not s[0].isalpha(): + s = "x_" + s + s = s[:41].rstrip("_") + if not extensions.NAME_RX.match(s): + raise ValueError(f"{name!r} can't be a name") + return s + + +def frontmatter(text): + """(head, body) of Markdown with YAML frontmatter; head {} when there is none.""" + text = text.lstrip("") + m = re.match(r"^---\s*\n(.*?)\n---\s*(?:\n(.*))?$", text, re.S) + if not m: + return {}, text.strip() + try: + head = yaml.safe_load(m.group(1)) or {} + except yaml.YAMLError: + head = {} + return (head if isinstance(head, dict) else {}), (m.group(2) or "").strip() + + +def _text(raw): + if raw is None: + return None + try: + return raw.decode("utf-8") + except UnicodeDecodeError: + return None + + +def _escape(text): + """Text from elsewhere, with what reads as a Lens {{parameter}} broken up, so it stays as written.""" + return extensions.VAR_RX.sub(lambda m: "{ {" + m.group(0)[2:-2] + "} }", text) + + +def _skill(found, folder, report): + """A skill folder (SKILL.md and its files) as a Lens skill item.""" + pre = f"{folder}/" if folder else "" + raw = found.get(f"{pre}SKILL.md") or found.get(f"{pre}skill.md") + head, body = frontmatter(_text(raw) or "") + name = head.get("name") or posixpath.basename(folder) or "skill" + desc = str(head.get("description") or "").strip() + if not body or not desc: + report.append({"kind": "skill", "name": str(name), "why": "its SKILL.md has no description or no instructions"}) + return None + extra, skipped = {}, 0 + for path, data in sorted(found.items()): + if not path.startswith(pre) or path[len(pre) :].lower() == "skill.md": + continue + rel = path[len(pre) :] + text = _text(data) + if text is None or len(extra) >= extensions.MAX_FILES or sum(map(len, extra.values())) + len(text) > extensions.MAX_FILES_CHARS: + skipped += 1 + continue + extra[rel] = text + if skipped: + report.append({"kind": "file", "name": str(name), "why": f"{skipped} of its files aren't text, or are past the size Lens keeps"}) + return store.clean( + { + "kind": "skill", + "name": slug(name), + "description": desc[:1024], + "spec": store.clean({"when": desc[:1024], "instructions": body[: extensions.MAX_SKILL], "files": extra or None}), + } + ) + + +def _command(path, raw, report): + head, body = frontmatter(_text(raw) or "") + stem = posixpath.splitext(posixpath.basename(path))[0] + if not body: + report.append({"kind": "command", "name": stem, "why": "it has no text"}) + return None + desc = str(head.get("description") or f"the /{stem} command").strip() + lead = f"This is the /{stem} command. $ARGUMENTS stands for what the person asked for.\n\n" + return { + "kind": "skill", + "name": slug(stem), + "description": desc[:1024], + "spec": {"when": f"someone asks for /{stem}, or for this: {desc}"[:1024], "instructions": (lead + body)[: extensions.MAX_SKILL]}, + } + + +def _agent(path, raw, report): + head, body = frontmatter(_text(raw) or "") + name = head.get("name") or posixpath.splitext(posixpath.basename(path))[0] + desc = str(head.get("description") or "").strip() + if not body or not desc: + report.append({"kind": "agent", "name": str(name), "why": "it has no description or no instructions"}) + return None + return { + "kind": "tool", + "name": slug(name), + "description": desc[:1024], + "spec": { + "params": [{"name": "task", "kind": "text", "required": True, "description": "what to ask of it, with what it needs to know"}], + "effect": "read", + "run": {"type": "prompt", "system": _escape(body)[: extensions.MAX_TEXT], "prompt": "{{task}}"}, + }, + } + + +def _paths(value): + if value is None: + return [] + return [value] if isinstance(value, str) else [v for v in value if isinstance(v, str)] if isinstance(value, list) else [] + + +def _norm(p): + p = posixpath.normpath(p.lstrip("/")) if p else "." + return "" if p == "." else p + + +def read_plugin(found, entry=None): + """A plugin folder's files ({path: bytes}) as a Lens plugin manifest, and what was left out: (manifest, skipped). + `entry` is its marketplace entry, which describes it when there's no plugin.json (strict: false).""" + entry = dict(entry or {}) + meta = {} + if PLUGIN_JSON in found: + try: + meta = json.loads(found[PLUGIN_JSON]) + except ValueError: + raise ValueError("its .claude-plugin/plugin.json isn't valid JSON") from None + if not isinstance(meta, dict): + meta = {} + meta = {**meta, **{k: v for k, v in entry.items() if k not in ("source",)}} + report, items = [], [] + dirs = sorted({posixpath.dirname(p) for p in found if posixpath.basename(p).lower() == "skill.md"}) + + # skills: the folders listed (a marketplace lists the ones each plugin of a shared folder has), else the ones + # under skills/, and the folder itself when it's one skill + skill_dirs = [] + for p in _paths(meta.get("skills")): + p = _norm(p) + skill_dirs += [d for d in dirs if d == p or (posixpath.dirname(d) == p)] + if meta.get("skills") is None: + skill_dirs += [d for d in dirs if posixpath.dirname(d) == "skills"] + if "" in dirs: + skill_dirs.append("") + for d in dict.fromkeys(skill_dirs): + sub = {p: b for p, b in found.items() if (p.startswith(d + "/") if d else True)} + if d == "": # the folder is itself a skill: not the plugin's other parts + sub = {p: b for p, b in sub.items() if not p.startswith((".claude-plugin/", "skills/", "commands/", "agents/", "hooks/"))} + it = _skill(sub, d, report) + if it: + items.append(it) + + def markdown(field, default): + out = [] + for p in _paths(meta.get(field)) + [default]: + p = _norm(p) + if p.endswith(".md") and p in found: + out.append(p) + else: + out += sorted(f for f in found if posixpath.dirname(f) == p and f.endswith(".md")) + return list(dict.fromkeys(out)) + + for p in markdown("commands", "commands"): + it = _command(p, found[p], report) + if it: + items.append(it) + for p in markdown("agents", "agents"): + it = _agent(p, found[p], report) + if it: + items.append(it) + + if meta.get("hooks") or "hooks/hooks.json" in found: + report.append( + {"kind": "hooks", "name": "hooks", "why": "Claude Code's hooks run commands on the computer; Lens hooks are made in Lens"} + ) + if meta.get("mcpServers") or ".mcp.json" in found: + report.append({"kind": "mcp", "name": "MCP servers", "why": "MCP servers from plugins aren't read yet"}) + + # names: each item's own, made different where two are the same + seen = set() + for it in items: + base, n = it["name"], 2 + while it["name"] in seen: + suffix = f"_{n}" + it["name"], n = base[: 41 - len(suffix)] + suffix, n + 1 + seen.add(it["name"]) + if not items: + raise ValueError("nothing in it Lens can use: no skills, commands or agents" + (f" ({report[0]['why']})" if report else "")) + if len(items) > extensions.MAX_ITEMS: + raise ValueError(f"it has {len(items)} skills, commands and agents; a Lens plugin has at most {extensions.MAX_ITEMS}") + name = meta.get("name") or (items[0]["name"] if len(items) == 1 else "plugin") + desc = str(meta.get("description") or (items[0]["description"] if len(items) == 1 else "") or name).strip() + manifest = store.clean({"name": slug(name), "kind": "plugin", "title": str(name)[:80], "description": desc[:1024], "items": items}) + return manifest, report + + +# ---------- marketplaces ---------- +def _row(db, mid): + d = db.one(f"SELECT {FIELDS}, plugins FROM $r", r=R("marketplace", int(mid))) + if not d: + raise KeyError(mid) + return d + + +def _index(cfg, src): + """A marketplace's marketplace.json, read and checked.""" + if src["host"] == "url": + raw = _get(cfg, src["url"], MAX_INDEX) + elif src["host"] in ("github", "gitlab"): + raw = _get(cfg, _raw_url(src, MANIFEST), MAX_INDEX) + else: + raise ValueError("a marketplace is a repository, or a link to its marketplace.json") + try: + m = json.loads(raw) + except ValueError: + raise ValueError("its marketplace.json isn't valid JSON") from None + if not isinstance(m, dict) or not isinstance(m.get("plugins"), list): + raise ValueError("its marketplace.json has no list of plugins") + plugins = [] + for p in m["plugins"][:2000]: + if isinstance(p, dict) and isinstance(p.get("name"), str): + plugins.append(json.loads(json.dumps(p))) + return { + "name": str(m.get("name") or src.get("repo") or "marketplace")[:100], + "description": str((m.get("metadata") or {}).get("description") or m.get("description") or "")[:1000] or None, + "metadata": m.get("metadata") if isinstance(m.get("metadata"), dict) else None, + "plugins": plugins, + } + + +def add(db, cfg, me, source, title=None): + """An admin adds a marketplace by its address; its id. Its list of plugins is read now and on refresh.""" + if not me["admin"]: + raise PermissionError("only admins add marketplaces") + src = parse_source(source) + if src["host"] == "archive": + raise ValueError("a marketplace is a repository, or a link to its marketplace.json") + shown = show_source(src) + for d in db.rows("SELECT source FROM marketplace"): + if show_source(d["source"]) == shown: + raise ValueError("that marketplace is already added") + got = _index(cfg, src) + mid, t = db.next_id("marketplace"), store.now() + db.q( + "CREATE $r CONTENT $d", + r=R("marketplace", mid), + d=store.clean( + {**got, "title": (title or "").strip()[:80] or None, "source": src, "added_by": me["email"], "created_at": t, "fetched_at": t} + ), + ) + return mid + + +def refresh(db, cfg, me, mid): + if not me["admin"]: + raise PermissionError("only admins refresh marketplaces") + d = _row(db, mid) + got = _index(cfg, d["source"]) + db.q("UPDATE $r MERGE $d", r=R("marketplace", d["id"]), d={**got, "fetched_at": store.now()}) + + +def remove(db, me, mid): + """Takes the marketplace away; what was installed from it stays.""" + if not me["admin"]: + raise PermissionError("only admins remove marketplaces") + _row(db, mid) + db.q("DELETE $r", r=R("marketplace", int(mid))) + + +def listing(db): + out = [] + for d in db.rows(f"SELECT {FIELDS}, array::len(plugins ?? []) AS count FROM marketplace ORDER BY id"): + out.append( + store.clean( + { + "id": d["id"], + "name": d["name"], + "title": d.get("title"), + "description": d.get("description"), + "source": show_source(d["source"]), + "plugins": d.get("count") or 0, + "added_by": d.get("added_by"), + "fetched_at": d.get("fetched_at"), + } + ) + ) + return out + + +def suggested(db): + """Anthropic's marketplaces that aren't added yet.""" + have = {show_source(d["source"]) for d in db.rows("SELECT source FROM marketplace")} + return [ + {**a, "source": show_source(parse_source(a["source"]))} for a in ANTHROPIC if show_source(parse_source(a["source"])) not in have + ] + + +def _installed(db, me): + """{(marketplace, plugin): extension id} for this person's installs.""" + out = {} + for d in db.rows( + "SELECT record::id(id) AS id, source FROM extension WHERE deleted_at = NONE AND owner = $o AND source != NONE", o=me["id"] + ): + s = d.get("source") or {} + if s.get("marketplace"): + out[(s["marketplace"], s.get("plugin"))] = d["id"] + return out + + +def plugins(db, me, mid): + """A marketplace's plugins, each saying whether Lens can install it and whether this person has.""" + d = _row(db, mid) + mine = _installed(db, me) + out = [] + for p in d.get("plugins") or []: + why = None + try: + plugin_source(p, {**d["source"], "metadata": d.get("metadata")}) + except ValueError as e: + why = str(e) + author = p.get("author") + out.append( + store.clean( + { + "name": p["name"], + "description": p.get("description") if isinstance(p.get("description"), str) else None, + "version": str(p["version"]) if p.get("version") else None, + "category": p.get("category") if isinstance(p.get("category"), str) else None, + "author": author.get("name") if isinstance(author, dict) else author if isinstance(author, str) else None, + "homepage": p.get("homepage") if isinstance(p.get("homepage"), str) else None, + "installable": why is None, + "why_not": why, + "installed": mine.get((d["id"], p["name"])), + } + ) + ) + return out + + +def _entry(db, mid, name): + d = _row(db, mid) + for p in d.get("plugins") or []: + if p["name"] == name: + src = plugin_source(p, {**d["source"], "metadata": d.get("metadata")}) + return d, p, src + raise KeyError(name) + + +def _read(cfg, src, entry=None): + found = files(cfg, src) + # a plugin's own plugin.json describes it, unless the marketplace says it describes it (strict: false) + if entry and entry.get("strict") is not False and PLUGIN_JSON in found: + entry = {k: v for k, v in entry.items() if k in ("name", "description", "version")} + return read_plugin(found, entry) + + +def preview(db, cfg, me, mid, name): + """What installing a marketplace's plugin would add to the assistant, and what it leaves out.""" + _, p, src = _entry(db, mid, name) + manifest, skipped = _read(cfg, src, p) + extensions.check_manifest(manifest, me, db) + return {"manifest": manifest, "skipped": skipped, "source": show_source(src)} + + +def _save(db, me, manifest, source, existing=None): + if existing: + n = extensions.save_version(db, me, existing, manifest, f"from {source.get('url')}", origin="import") + db.q("UPDATE $r MERGE $d", r=R("extension", existing), d={"source": source}) + return existing, n + return extensions.create(db, me, manifest, origin="import", source=source), 1 + + +def install(db, cfg, me, mid, name): + """Install a marketplace's plugin as this person's own plugin (private until they share it); again, a new version. + (extension id, version).""" + d, p, src = _entry(db, mid, name) + manifest, _ = _read(cfg, src, p) + source = store.clean( + {"marketplace": d["id"], "plugin": p["name"], "version": str(p["version"]) if p.get("version") else None, "url": show_source(src)} + ) + return _save(db, me, manifest, source, _installed(db, me).get((d["id"], p["name"]))) + + +def _same(db, me, key, value): + """This person's extension imported from the same link or file, which importing again gives a new version.""" + for d in db.rows( + "SELECT record::id(id) AS id, source FROM extension WHERE deleted_at = NONE AND owner = $o AND source != NONE", o=me["id"] + ): + s = d.get("source") or {} + if s.get(key) == value and not s.get("marketplace"): + return d["id"] + return None + + +def import_link(db, cfg, me, link, dry=False): + """A plugin or skill from a link (a repository, a folder in one, or a .zip): previewed, or installed as this + person's own (again: a new version).""" + src = parse_source(link) + if src["host"] == "url": + raise ValueError("that's a marketplace: an admin adds it under Marketplaces") + manifest, skipped = read_plugin(files(cfg, src)) + url = show_source(src) + if dry: + extensions.check_manifest(manifest, me, db) + return {"manifest": manifest, "skipped": skipped, "source": url} + eid, n = _save(db, me, manifest, {"url": url}, _same(db, me, "url", url)) + return {"id": eid, "version": n, "skipped": skipped} + + +def import_archive(db, me, filename, data, dry=False): + """A plugin or skill from a .zip or .tar.gz (a .skill file is a .zip), sent as base64.""" + try: + raw = base64.b64decode(data, validate=True) + except ValueError: + raise ValueError("send the file as base64") from None + if len(raw) > MAX_UPLOAD: + raise ValueError(f"the file is larger than {MAX_UPLOAD // 2**20} MB") + name = (filename or "the file").strip()[:200] + manifest, skipped = read_plugin(_unpack(io.BytesIO(raw), name)) + if dry: + extensions.check_manifest(manifest, me, db) + return {"manifest": manifest, "skipped": skipped, "source": name} + eid, n = _save(db, me, manifest, {"file": name}, _same(db, me, "file", name)) + return {"id": eid, "version": n, "skipped": skipped} diff --git a/fastapi_backend/app/domain/store.py b/fastapi_backend/app/domain/store.py index de3e3f873..4773969a2 100644 --- a/fastapi_backend/app/domain/store.py +++ b/fastapi_backend/app/domain/store.py @@ -1063,6 +1063,7 @@ def close(self): "DEFINE TABLE IF NOT EXISTS extension SCHEMALESS", "DEFINE TABLE IF NOT EXISTS extension_version SCHEMALESS", "DEFINE INDEX IF NOT EXISTS extension_version_e ON extension_version FIELDS extension", + "DEFINE TABLE IF NOT EXISTS marketplace SCHEMALESS", # where plugins and skills are installed from (marketplace.py) # routines (scheduled syncs, pipelines and workflows) and the graph changes they make or propose "DEFINE TABLE IF NOT EXISTS seed SCHEMALESS", # what has been seeded once: seed:routines "DEFINE TABLE IF NOT EXISTS routine SCHEMALESS", diff --git a/fastapi_backend/app/schemas/extensions.py b/fastapi_backend/app/schemas/extensions.py index 156c4efa9..f8391af3f 100644 --- a/fastapi_backend/app/schemas/extensions.py +++ b/fastapi_backend/app/schemas/extensions.py @@ -35,7 +35,8 @@ class Extension(ResponseModel): current: int version: int spec: dict[str, Any] = Field(description="the tool, skill, hook or plugin itself, as its manifest has it") - origin: str | None = Field(None, description="how this version was made: code, canvas or chat") + origin: str | None = Field(None, description="how this version was made: code, canvas, chat or import") + source: dict[str, Any] | None = Field(None, description="where it was imported from: a marketplace's plugin, a link or a file") updated_at: str | None = None @@ -82,3 +83,66 @@ class ExtensionTest(RequestModel): class ExtensionTestResult(ResponseModel): output: Any = None + + +class Marketplace(ResponseModel): + id: int + name: str = Field(description="the name its marketplace.json gives it") + title: str | None = None + description: str | None = None + source: str = Field(description="where it is: a repository, or a link to its marketplace.json") + plugins: int = Field(description="how many plugins it lists") + added_by: str | None = None + fetched_at: str | None = None + + +class MarketplaceSuggestion(ResponseModel): + name: str + title: str + source: str + + +class MarketplaceCreate(RequestModel): + source: str = Field(description="owner/repo on GitHub, a GitHub or GitLab link, or a link to a marketplace.json") + title: str | None = None + + +class MarketplacePlugin(ResponseModel): + name: str + description: str | None = None + version: str | None = None + category: str | None = None + author: str | None = None + homepage: str | None = None + installable: bool = Field(description="whether Lens can read it (why_not says why when it can't)") + why_not: str | None = None + installed: int | None = Field(None, description="your extension installed from it, if you installed it") + + +class ExtensionSkipped(ResponseModel): + kind: str = Field(description="what was left out: hooks, mcp, skill, command, agent or file") + name: str + why: str + + +class ExtensionImportPreview(ResponseModel): + manifest: dict[str, Any] = Field(description="the plugin as Lens would install it") + skipped: list[ExtensionSkipped] = [] + source: str + + +class ExtensionImported(ResponseModel): + id: int + version: int + skipped: list[ExtensionSkipped] = [] + + +class ExtensionImportLink(RequestModel): + url: str = Field(description="a plugin or skill: a GitHub or GitLab repository or folder, or a .zip or .tar.gz link") + preview: bool = Field(False, description="say what it would install, without installing it") + + +class ExtensionImportArchive(RequestModel): + filename: str | None = None + data: str = Field(max_length=28_000_000, description="a .zip, .skill or .tar.gz with a plugin or a skill, as base64 (20 MB at most)") + preview: bool = False diff --git a/fastapi_backend/tests/api/test_marketplaces.py b/fastapi_backend/tests/api/test_marketplaces.py new file mode 100644 index 000000000..335537dfa --- /dev/null +++ b/fastapi_backend/tests/api/test_marketplaces.py @@ -0,0 +1,259 @@ +"""Marketplaces: plugins and skills in Anthropic's format, from a marketplace an admin added, a link or a .zip, +installed as extensions and used in conversations.""" + +from __future__ import annotations + +import base64 +import io +import json +import tarfile +import zipfile + +import pytest + +from app.domain import marketplace +from tests import fake_llm +from tests.api._assist import Assist, sse, start_llm + +PDF = """--- +name: pdf +description: Use this skill whenever the user wants to do anything with PDF files. +license: Proprietary +--- + +# PDF Processing Guide + +For forms, read FORMS.md and follow it. Keep {{this}} as written. +""" + +XLSX = """--- +name: xlsx +description: Spreadsheets of every kind. +--- + +Open the workbook first. +""" + +SKILLS_REPO = { + ".claude-plugin/marketplace.json": json.dumps( + { + "name": "anthropic-agent-skills", + "metadata": {"description": "Anthropic example skills"}, + "plugins": [ + {"name": "document-skills", "description": "Documents", "source": "./", "strict": False, "skills": ["./skills/pdf"]}, + {"name": "all-skills", "description": "Every one", "source": "./", "strict": False, "skills": ["./skills"]}, + { + "name": "acme-tools", + "description": "Acme's tools", + "source": {"source": "github", "repo": "acme/tools"}, + "version": "1.2.0", + }, + {"name": "from-npm", "source": {"source": "npm", "package": "x"}}, + ], + } + ), + "skills/pdf/SKILL.md": PDF, + "skills/pdf/FORMS.md": "Fill the form field by field.", + "skills/pdf/font.ttf": b"\x00\x01binary", + "skills/xlsx/SKILL.md": XLSX, + "README.md": "skills", +} + +TOOLS_REPO = { + ".claude-plugin/plugin.json": json.dumps({"name": "acme-tools", "description": "Acme's review kit", "version": "1.2.0"}), + "commands/review.md": "---\ndescription: Review a change\n---\n\nReview $ARGUMENTS for bugs.\n", + "agents/critic.md": "---\nname: critic\ndescription: A harsh critic of drafts\ntools: Read, Grep\n---\n\nYou are a critic. Be blunt.\n", + "skills/notes/SKILL.md": "---\nname: meeting-notes\ndescription: Writing up meeting notes\n---\n\nUse three headings.\n", + "hooks/hooks.json": json.dumps({"hooks": {"PreToolUse": []}}), + ".mcp.json": json.dumps({"mcpServers": {"x": {"command": "npx"}}}), +} + + +def tarball(files, top="repo-main"): + buf = io.BytesIO() + with tarfile.open(fileobj=buf, mode="w:gz") as t: + for path, text in files.items(): + data = text if isinstance(text, bytes) else text.encode() + info = tarfile.TarInfo(f"{top}/{path}") + info.size = len(data) + t.addfile(info, io.BytesIO(data)) + return buf.getvalue() + + +def zipped(files): + buf = io.BytesIO() + with zipfile.ZipFile(buf, "w") as z: + for path, text in files.items(): + z.writestr(path, text) + return buf.getvalue() + + +@pytest.fixture +def web(monkeypatch): + """The web, as far as these tests go: {url: bytes}, and every address asked for.""" + pages = { + "https://raw.githubusercontent.com/anthropics/skills/HEAD/.claude-plugin/marketplace.json": SKILLS_REPO[ + ".claude-plugin/marketplace.json" + ].encode(), + "https://codeload.github.com/anthropics/skills/tar.gz/HEAD": tarball(SKILLS_REPO), + "https://codeload.github.com/acme/tools/tar.gz/HEAD": tarball(TOOLS_REPO), + "https://example.com/notes.zip": zipped({"notes/SKILL.md": TOOLS_REPO["skills/notes/SKILL.md"]}), + } + asked = [] + + def fetch(cfg, url, out, limit): + asked.append(url) + if url not in pages: + raise ValueError("example.com answered 404 Not Found") + out.write(pages[url]) + + monkeypatch.setattr(marketplace, "_fetch", fetch) + marketplace._cache.clear() + return asked + + +@pytest.fixture +def llm(cfg): + srv = start_llm(cfg) + yield fake_llm.Handler + srv.shutdown() + + +@pytest.fixture +def app(cfg, db, llm): + from app.main import create_app + + return create_app(cfg, db, background=False) + + +def test_sources_people_paste(): + p = marketplace.parse_source + assert p("anthropics/skills") == {"host": "github", "repo": "anthropics/skills"} + assert p("anthropics/skills@v2") == {"host": "github", "repo": "anthropics/skills", "ref": "v2"} + assert p("https://github.com/anthropics/skills/tree/main/skills/pdf") == { + "host": "github", + "repo": "anthropics/skills", + "ref": "main", + "path": "skills/pdf", + } + assert p("https://github.com/a/b/blob/main/.claude-plugin/marketplace.json") == {"host": "github", "repo": "a/b", "ref": "main"} + assert p("https://raw.githubusercontent.com/a/b/main/.claude-plugin/marketplace.json") == { + "host": "github", + "repo": "a/b", + "ref": "main", + } + assert p("https://gitlab.com/g/sub/repo/-/tree/dev/x") == {"host": "gitlab", "repo": "g/sub/repo", "ref": "dev", "path": "x"} + assert p("https://example.com/m.json")["host"] == "url" + assert p("https://example.com/a.skill")["host"] == "archive" + for bad in ("ftp://x/y", "just words", "https://example.com/page"): + with pytest.raises(ValueError): + p(bad) + market = {"host": "github", "repo": "o/m", "ref": "main"} + assert marketplace.plugin_source({"source": "./plugins/x"}, market)["path"] == "plugins/x" + with pytest.raises(ValueError, match="outside"): + marketplace.plugin_source({"source": "../../x"}, market) + git = {"source": "git-subdir", "url": "https://github.com/o/r.git", "path": "plugins/p", "ref": "v1", "sha": "abc"} + assert marketplace.plugin_source({"source": git}, market) == {"host": "github", "repo": "o/r", "ref": "abc", "path": "plugins/p"} + + +def test_a_marketplace_an_admin_adds(app, db, cfg, folder, new_client, web): + s = Assist(app, db, cfg, folder, new_client) + c, h = s.cl["editor"] + ca, ha = s.cl["admin"] + assert [m["source"] for m in c.get("/api/v1/marketplaces/suggested", headers=h).json()] == [ + "https://github.com/anthropics/skills", + "https://github.com/anthropics/claude-plugins-official", + ] + # adding one is the admin's approval + assert c.post("/api/v1/marketplaces", headers=h, json={"source": "anthropics/skills"}).status_code == 403 + mid = ca.post("/api/v1/marketplaces", headers=ha, json={"source": "anthropics/skills"}).json()["id"] + assert ca.post("/api/v1/marketplaces", headers=ha, json={"source": "https://github.com/anthropics/skills"}).status_code == 400 + assert len(c.get("/api/v1/marketplaces/suggested", headers=h).json()) == 1 + got = c.get("/api/v1/marketplaces", headers=h).json() + assert [(m["name"], m["plugins"], m["description"]) for m in got] == [("anthropic-agent-skills", 4, "Anthropic example skills")] + + plugins = {p["name"]: p for p in c.get(f"/api/v1/marketplaces/{mid}/plugins", headers=h).json()} + assert plugins["document-skills"]["installable"] and plugins["acme-tools"]["version"] == "1.2.0" + assert not plugins["from-npm"]["installable"] and "npm" in plugins["from-npm"]["why_not"] + + # previewed: what it adds and leaves out, nothing installed + pre = c.post(f"/api/v1/marketplaces/{mid}/plugins/document-skills/preview", headers=h).json() + assert [(i["kind"], i["name"]) for i in pre["manifest"]["items"]] == [("skill", "pdf")] + pdf = pre["manifest"]["items"][0]["spec"] + assert pdf["files"] == {"FORMS.md": "Fill the form field by field."} and "Keep {{this}}" in pdf["instructions"] + assert [x["kind"] for x in pre["skipped"]] == ["file"] + assert c.get("/api/v1/extensions", headers=h).json() == [] + + out = c.post(f"/api/v1/marketplaces/{mid}/plugins/document-skills/install", headers=h).json() + assert out["version"] == 1 + ext = c.get(f"/api/v1/extensions/{out['id']}", headers=h).json() + assert (ext["kind"], ext["name"], ext["visibility"], ext["origin"]) == ("plugin", "document_skills", "private", "import") + assert ext["source"] == {"marketplace": mid, "plugin": "document-skills", "url": "https://github.com/anthropics/skills"} + assert c.get(f"/api/v1/marketplaces/{mid}/plugins", headers=h).json()[0]["installed"] == out["id"] + # again: a new version of the same extension + assert c.post(f"/api/v1/marketplaces/{mid}/plugins/document-skills/install", headers=h).json() == { + "id": out["id"], + "version": 2, + "skipped": [], + } + # a plugin from another repository, with what Lens leaves out named + pre = c.post(f"/api/v1/marketplaces/{mid}/plugins/acme-tools/preview", headers=h).json() + assert {x["kind"] for x in pre["skipped"]} == {"hooks", "mcp"} + # a skill another extension already has can't be installed twice + clash = c.post(f"/api/v1/marketplaces/{mid}/plugins/all-skills/install", headers=h) + assert clash.status_code == 400 and "already called pdf" in clash.json()["detail"] + + assert ca.delete(f"/api/v1/marketplaces/{mid}", headers=ha).status_code == 200 + assert c.get("/api/v1/marketplaces", headers=h).json() == [] + assert c.get(f"/api/v1/extensions/{out['id']}", headers=h).status_code == 200 # what was installed stays + + +def test_imported_from_a_link_or_a_file(app, db, cfg, folder, new_client, web): + s = Assist(app, db, cfg, folder, new_client) + c, h = s.cl["editor"] + pre = c.post("/api/v1/extensions/import", headers=h, json={"url": "acme/tools", "preview": True}).json() + items = {i["name"]: i for i in pre["manifest"]["items"]} + assert {n: i["kind"] for n, i in items.items()} == {"meeting_notes": "skill", "review": "skill", "critic": "tool"} + assert items["review"]["spec"]["when"] == "someone asks for /review, or for this: Review a change" + critic = items["critic"]["spec"] + assert critic["run"] == {"type": "prompt", "system": "You are a critic. Be blunt.", "prompt": "{{task}}"} + assert pre["manifest"]["name"] == "acme_tools" and c.get("/api/v1/extensions", headers=h).json() == [] + + out = c.post("/api/v1/extensions/import", headers=h, json={"url": "https://github.com/acme/tools"}).json() + assert out["version"] == 1 and {x["kind"] for x in out["skipped"]} == {"hooks", "mcp"} + assert c.post("/api/v1/extensions/import", headers=h, json={"url": "https://github.com/acme/tools"}).json()["version"] == 2 + bad = c.post("/api/v1/extensions/import", headers=h, json={"url": "https://github.com/acme/missing"}) + assert bad.status_code == 400 and "404" in bad.json()["detail"] + assert c.post("/api/v1/extensions/import", headers=h, json={"url": "https://example.com/m.json"}).status_code == 400 + + # a skill folder zipped up (as claude.ai exports one): one skill, in a plugin of its own + data = base64.b64encode(zipped({"notes/SKILL.md": PDF.replace("name: pdf", "name: pdf-forms"), "notes/FORMS.md": "x"})).decode() + got = c.post("/api/v1/extensions/import/archive", headers=h, json={"filename": "pdf.skill", "data": data}).json() + ext = c.get(f"/api/v1/extensions/{got['id']}", headers=h).json() + assert ext["name"] == "pdf_forms" and ext["spec"]["items"][0]["spec"]["files"] == {"FORMS.md": "x"} + junk = c.post("/api/v1/extensions/import/archive", headers=h, json={"filename": "x.zip", "data": base64.b64encode(b"nope").decode()}) + assert junk.status_code == 400 + + +def call(i, name, args): + return {"id": f"c{i}", "type": "function", "function": {"name": name, "arguments": json.dumps(args)}} + + +def test_a_skills_files_in_a_conversation(app, db, cfg, folder, new_client, web, llm): + s = Assist(app, db, cfg, folder, new_client) + c, h = s.cl["editor"] + c.post("/api/v1/extensions/import", headers=h, json={"url": "https://github.com/anthropics/skills/tree/HEAD/skills/pdf"}) + llm.seen.clear() + llm.tool_script = [ + {"content": "", "tool_calls": [call(1, "use_skill", {"name": "pdf"})]}, + {"content": "", "tool_calls": [call(2, "use_skill", {"name": "pdf", "file": "FORMS.md"})]}, + {"content": "Done."}, + ] + cid = c.post("/api/v1/chats", headers=h, json={}).json()["id"] + ev = sse(c.post(f"/api/v1/chats/{cid}/messages", headers=h, json={"content": "Fill this PDF form"}).text) + assert [x["summary"] for x in ev["step"]] == ["Read the skill pdf", "Read FORMS.md from the skill pdf"] + first = [b for b in llm.seen if b.get("tools")][0] + use = next(t for t in first["tools"] if t["function"]["name"] == "use_skill") + assert "file" in use["function"]["parameters"]["properties"] + tool_msgs = [m["content"] for b in llm.seen if b.get("tools") for m in b["messages"] if m["role"] == "tool"] + assert '"files": ["FORMS.md"]' in tool_msgs[0] and "field by field" in tool_msgs[-1] diff --git a/nextjs-frontend/__tests__/marketplace-ui.test.tsx b/nextjs-frontend/__tests__/marketplace-ui.test.tsx new file mode 100644 index 000000000..557131336 --- /dev/null +++ b/nextjs-frontend/__tests__/marketplace-ui.test.tsx @@ -0,0 +1,129 @@ +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import { fireEvent, render, screen, waitFor } from "@testing-library/react"; +import "@testing-library/jest-dom"; +import type { ReactNode } from "react"; + +import { Extensions } from "@/app/openapi-client"; +import { MarketplacePage } from "@/components/extensions/marketplace-page"; +import { TooltipProvider } from "@/components/ui/tooltip"; + +jest.mock("@/app/openapi-client", () => ({ + Extensions: { + listMarketplaces: jest.fn(), + suggestedMarketplaces: jest.fn(), + addMarketplace: jest.fn(), + refreshMarketplace: jest.fn(), + deleteMarketplace: jest.fn(), + listMarketplacePlugins: jest.fn(), + previewMarketplacePlugin: jest.fn(), + installMarketplacePlugin: jest.fn(), + importLink: jest.fn(), + importArchive: jest.fn(), + }, +})); +jest.mock("next-auth/react", () => ({ useSession: () => ({ data: { accessToken: "t" } }) })); +const toast = jest.fn(); +jest.mock("@/components/ui/toast", () => ({ useToast: () => toast })); +let admin = true; +jest.mock("@/lib/hooks/session", () => ({ useArchive: () => ({ admin }) })); + +const ok = (data: unknown) => Promise.resolve({ data, response: { ok: true, status: 200 } }); +const m = (f: unknown) => f as jest.Mock; + +function wrap(ui: ReactNode) { + const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } }); + return render( + + {ui} + , + ); +} + +const market = { + id: 1, + name: "anthropic-agent-skills", + title: "Anthropic skills", + description: "Anthropic example skills", + source: "https://github.com/anthropics/skills", + plugins: 2, +}; +const plugins = [ + { name: "document-skills", description: "Documents", installable: true, installed: null }, + { name: "from-npm", installable: false, why_not: "npm" }, +]; +const preview = { + manifest: { + name: "document_skills", + title: "document-skills", + description: "Documents", + items: [{ kind: "skill", name: "pdf", description: "PDF files" }], + }, + skipped: [{ kind: "hooks", name: "hooks", why: "they run commands" }], + source: "https://github.com/anthropics/skills", +}; + +beforeEach(() => { + jest.clearAllMocks(); + admin = true; + m(Extensions.listMarketplaces).mockReturnValue(ok([market])); + m(Extensions.suggestedMarketplaces).mockReturnValue( + ok([ + { + name: "claude-plugins-official", + title: "Anthropic plugins", + source: "https://github.com/anthropics/claude-plugins-official", + }, + ]), + ); + m(Extensions.listMarketplacePlugins).mockReturnValue(ok(plugins)); + m(Extensions.previewMarketplacePlugin).mockReturnValue(ok(preview)); + m(Extensions.installMarketplacePlugin).mockReturnValue(ok({ id: 9, version: 1, skipped: [] })); + m(Extensions.addMarketplace).mockReturnValue(ok({ ok: true, id: 2 })); + m(Extensions.importLink).mockReturnValue(ok(preview)); +}); + +test("browses a marketplace, previews a plugin and installs it", async () => { + wrap(); + expect(await screen.findByText("document-skills")).toBeInTheDocument(); + expect(screen.getByText("Not supported yet")).toBeInTheDocument(); + fireEvent.click(screen.getByRole("button", { name: "Install" })); + expect(await screen.findByText("pdf")).toBeInTheDocument(); + expect(screen.getByText(/they run commands/)).toBeInTheDocument(); + expect(Extensions.previewMarketplacePlugin).toHaveBeenCalledWith( + expect.objectContaining({ path: { mid: 1, name: "document-skills" } }), + ); + const install = screen.getAllByRole("button", { name: "Install" }).at(-1)!; + await waitFor(() => expect(install).toBeEnabled()); + fireEvent.click(install); + await waitFor(() => expect(Extensions.installMarketplacePlugin).toHaveBeenCalled()); + await waitFor(() => expect(toast).toHaveBeenCalledWith(expect.objectContaining({ title: "Installed" }))); +}); + +test("an admin adds a suggested marketplace; others don't see the form", async () => { + const { unmount } = wrap(); + fireEvent.click(await screen.findByRole("button", { name: "Add Anthropic plugins" })); + await waitFor(() => + expect(Extensions.addMarketplace).toHaveBeenCalledWith( + expect.objectContaining({ + body: { source: "https://github.com/anthropics/claude-plugins-official", title: "Anthropic plugins" }, + }), + ), + ); + unmount(); + admin = false; + wrap(); + expect(await screen.findByText("document-skills")).toBeInTheDocument(); + expect(screen.queryByLabelText("Add a marketplace")).not.toBeInTheDocument(); +}); + +test("imports from a link after a preview", async () => { + wrap(); + fireEvent.change(screen.getByLabelText("From a link"), { target: { value: "acme/tools" } }); + fireEvent.click(screen.getByRole("button", { name: "Preview" })); + await waitFor(() => + expect(Extensions.importLink).toHaveBeenCalledWith( + expect.objectContaining({ body: { url: "acme/tools", preview: true } }), + ), + ); + expect(await screen.findByText("pdf")).toBeInTheDocument(); +}); diff --git a/nextjs-frontend/app/(app)/extensions/marketplace/page.tsx b/nextjs-frontend/app/(app)/extensions/marketplace/page.tsx new file mode 100644 index 000000000..03bdf061c --- /dev/null +++ b/nextjs-frontend/app/(app)/extensions/marketplace/page.tsx @@ -0,0 +1,10 @@ +import type { Metadata } from "next"; + +import { MarketplacePage } from "@/components/extensions/marketplace-page"; + +export const metadata: Metadata = { title: "Marketplace" }; + +/** Skills and plugins from marketplaces in Anthropic's format, links and files. */ +export default function Page() { + return ; +} diff --git a/nextjs-frontend/app/openapi-client/index.ts b/nextjs-frontend/app/openapi-client/index.ts index 17ad6f652..40c0474af 100644 --- a/nextjs-frontend/app/openapi-client/index.ts +++ b/nextjs-frontend/app/openapi-client/index.ts @@ -77,6 +77,11 @@ export type { AddLoginProviderErrors, AddLoginProviderResponse, AddLoginProviderResponses, + AddMarketplaceData, + AddMarketplaceError, + AddMarketplaceErrors, + AddMarketplaceResponse, + AddMarketplaceResponses, AddPasskeyData, AddPasskeyError, AddPasskeyErrors, @@ -587,6 +592,11 @@ export type { DeleteLoginErrors, DeleteLoginResponse, DeleteLoginResponses, + DeleteMarketplaceData, + DeleteMarketplaceError, + DeleteMarketplaceErrors, + DeleteMarketplaceResponse, + DeleteMarketplaceResponses, DeleteNamespaceCollectionData, DeleteNamespaceCollectionError, DeleteNamespaceCollectionErrors, @@ -762,6 +772,11 @@ export type { Extension, ExtensionCreate, ExtensionDetail, + ExtensionImportArchive, + ExtensionImported, + ExtensionImportLink, + ExtensionImportPreview, + ExtensionSkipped, ExtensionTest, ExtensionTestResult, ExtensionUpdate, @@ -1230,6 +1245,11 @@ export type { IiifPanel, IiifUrl, IiifValidation, + ImportArchiveData, + ImportArchiveError, + ImportArchiveErrors, + ImportArchiveResponse, + ImportArchiveResponses, ImportedAs, ImportFromSourceData, ImportFromSourceError, @@ -1246,6 +1266,11 @@ export type { ImportIiifErrors, ImportIiifResponse, ImportIiifResponses, + ImportLinkData, + ImportLinkError, + ImportLinkErrors, + ImportLinkResponse, + ImportLinkResponses, ImportNamespaceRdfData, ImportNamespaceRdfError, ImportNamespaceRdfErrors, @@ -1281,6 +1306,11 @@ export type { IndexSemanticErrors, IndexSemanticResponse, IndexSemanticResponses, + InstallMarketplacePluginData, + InstallMarketplacePluginError, + InstallMarketplacePluginErrors, + InstallMarketplacePluginResponse, + InstallMarketplacePluginResponses, IpGroup, IpGroupCreate, IpGroups, @@ -1469,6 +1499,14 @@ export type { ListLoginsData, ListLoginsResponse, ListLoginsResponses, + ListMarketplacePluginsData, + ListMarketplacePluginsError, + ListMarketplacePluginsErrors, + ListMarketplacePluginsResponse, + ListMarketplacePluginsResponses, + ListMarketplacesData, + ListMarketplacesResponse, + ListMarketplacesResponses, ListMembersData, ListMembersError, ListMembersErrors, @@ -1683,6 +1721,10 @@ export type { MarkEntitiesNotSameErrors, MarkEntitiesNotSameResponse, MarkEntitiesNotSameResponses, + Marketplace, + MarketplaceCreate, + MarketplacePlugin, + MarketplaceSuggestion, MatterbridgeRoom, MatterbridgeStatus, Me, @@ -1908,6 +1950,11 @@ export type { PreviewImportResponse, PreviewImportResponses, PreviewLine, + PreviewMarketplacePluginData, + PreviewMarketplacePluginError, + PreviewMarketplacePluginErrors, + PreviewMarketplacePluginResponse, + PreviewMarketplacePluginResponses, PreviewRoutesData, PreviewRoutesError, PreviewRoutesErrors, @@ -2026,6 +2073,11 @@ export type { RefreshData, RefreshError, RefreshErrors, + RefreshMarketplaceData, + RefreshMarketplaceError, + RefreshMarketplaceErrors, + RefreshMarketplaceResponse, + RefreshMarketplaceResponses, RefreshRequest, RefreshResponse, RefreshResponses, @@ -2473,6 +2525,9 @@ export type { StreamEventsError, StreamEventsErrors, StreamEventsResponses, + SuggestedMarketplacesData, + SuggestedMarketplacesResponse, + SuggestedMarketplacesResponses, Suggestion, SuggestTermsData, SuggestTermsError, diff --git a/nextjs-frontend/app/openapi-client/sdk.gen.ts b/nextjs-frontend/app/openapi-client/sdk.gen.ts index 04852ba01..26f5e53ac 100644 --- a/nextjs-frontend/app/openapi-client/sdk.gen.ts +++ b/nextjs-frontend/app/openapi-client/sdk.gen.ts @@ -23,6 +23,9 @@ import type { AddLoginProviderData, AddLoginProviderErrors, AddLoginProviderResponses, + AddMarketplaceData, + AddMarketplaceErrors, + AddMarketplaceResponses, AddPasskeyData, AddPasskeyErrors, AddPasskeyOptionsData, @@ -279,6 +282,9 @@ import type { DeleteLoginData, DeleteLoginErrors, DeleteLoginResponses, + DeleteMarketplaceData, + DeleteMarketplaceErrors, + DeleteMarketplaceResponses, DeleteNamespaceCollectionData, DeleteNamespaceCollectionErrors, DeleteNamespaceCollectionResponses, @@ -613,12 +619,18 @@ import type { HomeSuggestionsData, HomeSuggestionsErrors, HomeSuggestionsResponses, + ImportArchiveData, + ImportArchiveErrors, + ImportArchiveResponses, ImportFromSourceData, ImportFromSourceErrors, ImportFromSourceResponses, ImportIiifData, ImportIiifErrors, ImportIiifResponses, + ImportLinkData, + ImportLinkErrors, + ImportLinkResponses, ImportNamespaceRdfData, ImportNamespaceRdfErrors, ImportNamespaceRdfResponses, @@ -637,6 +649,9 @@ import type { IndexSemanticData, IndexSemanticErrors, IndexSemanticResponses, + InstallMarketplacePluginData, + InstallMarketplacePluginErrors, + InstallMarketplacePluginResponses, LinkEntityData, LinkEntityErrors, LinkEntityResponses, @@ -741,6 +756,11 @@ import type { ListLoginProvidersResponses, ListLoginsData, ListLoginsResponses, + ListMarketplacePluginsData, + ListMarketplacePluginsErrors, + ListMarketplacePluginsResponses, + ListMarketplacesData, + ListMarketplacesResponses, ListMembersData, ListMembersErrors, ListMembersResponses, @@ -938,6 +958,9 @@ import type { PreviewImportData, PreviewImportErrors, PreviewImportResponses, + PreviewMarketplacePluginData, + PreviewMarketplacePluginErrors, + PreviewMarketplacePluginResponses, PreviewRoutesData, PreviewRoutesErrors, PreviewRoutesResponses, @@ -984,6 +1007,9 @@ import type { RedeemTicketResponses, RefreshData, RefreshErrors, + RefreshMarketplaceData, + RefreshMarketplaceErrors, + RefreshMarketplaceResponses, RefreshResponses, RegeneratePodcastData, RegeneratePodcastErrors, @@ -1194,6 +1220,8 @@ import type { StreamEventsData, StreamEventsErrors, StreamEventsResponses, + SuggestedMarketplacesData, + SuggestedMarketplacesResponses, SuggestTermsData, SuggestTermsErrors, SuggestTermsResponses, @@ -7571,6 +7599,43 @@ export class Extensions { }); } + /** + * Import Link + * + * A plugin or skill in Anthropic's format from a link: a GitHub or GitLab repository or folder, or a .zip. With + * `preview`, what it would add and leave out; else installed as your own (importing it again: a new version). + */ + public static importLink( + options: Options, + ): RequestResult { + return (options.client ?? client).post({ + url: "/api/v1/extensions/import", + ...options, + headers: { + "Content-Type": "application/json", + ...options.headers, + }, + }); + } + + /** + * Import Archive + * + * A plugin or skill from a .zip, .skill or .tar.gz you send (as base64). + */ + public static importArchive( + options: Options, + ): RequestResult { + return (options.client ?? client).post({ + url: "/api/v1/extensions/import/archive", + ...options, + headers: { + "Content-Type": "application/json", + ...options.headers, + }, + }); + } + /** * Delete Extension * @@ -7653,6 +7718,120 @@ export class Extensions { }, }); } + + /** + * List Marketplaces + */ + public static listMarketplaces( + options?: Options, + ): RequestResult { + return (options?.client ?? client).get({ + url: "/api/v1/marketplaces", + ...options, + }); + } + + /** + * Add Marketplace + * + * Add a marketplace by its address; its list of plugins is read now. + */ + public static addMarketplace( + options: Options, + ): RequestResult { + return (options.client ?? client).post({ + url: "/api/v1/marketplaces", + ...options, + headers: { + "Content-Type": "application/json", + ...options.headers, + }, + }); + } + + /** + * Suggested Marketplaces + * + * Anthropic's marketplaces, when they aren't added yet. + */ + public static suggestedMarketplaces( + options?: Options, + ): RequestResult { + return (options?.client ?? client).get({ + url: "/api/v1/marketplaces/suggested", + ...options, + }); + } + + /** + * Refresh Marketplace + * + * Read its list of plugins again. + */ + public static refreshMarketplace( + options: Options, + ): RequestResult { + return (options.client ?? client).post({ + url: "/api/v1/marketplaces/{mid}/refresh", + ...options, + }); + } + + /** + * Delete Marketplace + * + * Takes it away; what was installed from it stays. + */ + public static deleteMarketplace( + options: Options, + ): RequestResult { + return (options.client ?? client).delete({ + url: "/api/v1/marketplaces/{mid}", + ...options, + }); + } + + /** + * List Marketplace Plugins + */ + public static listMarketplacePlugins( + options: Options, + ): RequestResult { + return (options.client ?? client).get({ + url: "/api/v1/marketplaces/{mid}/plugins", + ...options, + }); + } + + /** + * Preview Marketplace Plugin + * + * What installing it would add to the assistant, and what Lens leaves out. + */ + public static previewMarketplacePlugin( + options: Options, + ): RequestResult { + return (options.client ?? client).post< + PreviewMarketplacePluginResponses, + PreviewMarketplacePluginErrors, + ThrowOnError + >({ url: "/api/v1/marketplaces/{mid}/plugins/{name}/preview", ...options }); + } + + /** + * Install Marketplace Plugin + * + * Install it as your own plugin, private until you share it; installing it again brings its new version. + */ + public static installMarketplacePlugin( + options: Options, + ): RequestResult { + return (options.client ?? client).post< + InstallMarketplacePluginResponses, + InstallMarketplacePluginErrors, + ThrowOnError + >({ url: "/api/v1/marketplaces/{mid}/plugins/{name}/install", ...options }); + } } export class Chats { diff --git a/nextjs-frontend/app/openapi-client/types.gen.ts b/nextjs-frontend/app/openapi-client/types.gen.ts index fe9bd67f7..e594abbfe 100644 --- a/nextjs-frontend/app/openapi-client/types.gen.ts +++ b/nextjs-frontend/app/openapi-client/types.gen.ts @@ -3978,9 +3978,17 @@ export type Extension = { /** * Origin * - * how this version was made: code, canvas or chat + * how this version was made: code, canvas, chat or import */ origin?: string | null; + /** + * Source + * + * where it was imported from: a marketplace's plugin, a link or a file + */ + source?: { + [key: string]: unknown; + } | null; /** * Updated At */ @@ -4085,9 +4093,17 @@ export type ExtensionDetail = { /** * Origin * - * how this version was made: code, canvas or chat + * how this version was made: code, canvas, chat or import */ origin?: string | null; + /** + * Source + * + * where it was imported from: a marketplace's plugin, a link or a file + */ + source?: { + [key: string]: unknown; + } | null; /** * Updated At */ @@ -4105,6 +4121,107 @@ export type ExtensionDetail = { [key: string]: unknown; }; +/** + * ExtensionImportArchive + */ +export type ExtensionImportArchive = { + /** + * Filename + */ + filename?: string | null; + /** + * Data + * + * a .zip, .skill or .tar.gz with a plugin or a skill, as base64 (20 MB at most) + */ + data: string; + /** + * Preview + */ + preview?: boolean; +}; + +/** + * ExtensionImportLink + */ +export type ExtensionImportLink = { + /** + * Url + * + * a plugin or skill: a GitHub or GitLab repository or folder, or a .zip or .tar.gz link + */ + url: string; + /** + * Preview + * + * say what it would install, without installing it + */ + preview?: boolean; +}; + +/** + * ExtensionImportPreview + */ +export type ExtensionImportPreview = { + /** + * Manifest + * + * the plugin as Lens would install it + */ + manifest: { + [key: string]: unknown; + }; + /** + * Skipped + */ + skipped?: Array; + /** + * Source + */ + source: string; + [key: string]: unknown; +}; + +/** + * ExtensionImported + */ +export type ExtensionImported = { + /** + * Id + */ + id: number; + /** + * Version + */ + version: number; + /** + * Skipped + */ + skipped?: Array; + [key: string]: unknown; +}; + +/** + * ExtensionSkipped + */ +export type ExtensionSkipped = { + /** + * Kind + * + * what was left out: hooks, mcp, skill, command, agent or file + */ + kind: string; + /** + * Name + */ + name: string; + /** + * Why + */ + why: string; + [key: string]: unknown; +}; + /** * ExtensionTest */ @@ -7074,6 +7191,133 @@ export type ManifestChecked = { [key: string]: unknown; }; +/** + * Marketplace + */ +export type Marketplace = { + /** + * Id + */ + id: number; + /** + * Name + * + * the name its marketplace.json gives it + */ + name: string; + /** + * Title + */ + title?: string | null; + /** + * Description + */ + description?: string | null; + /** + * Source + * + * where it is: a repository, or a link to its marketplace.json + */ + source: string; + /** + * Plugins + * + * how many plugins it lists + */ + plugins: number; + /** + * Added By + */ + added_by?: string | null; + /** + * Fetched At + */ + fetched_at?: string | null; + [key: string]: unknown; +}; + +/** + * MarketplaceCreate + */ +export type MarketplaceCreate = { + /** + * Source + * + * owner/repo on GitHub, a GitHub or GitLab link, or a link to a marketplace.json + */ + source: string; + /** + * Title + */ + title?: string | null; +}; + +/** + * MarketplacePlugin + */ +export type MarketplacePlugin = { + /** + * Name + */ + name: string; + /** + * Description + */ + description?: string | null; + /** + * Version + */ + version?: string | null; + /** + * Category + */ + category?: string | null; + /** + * Author + */ + author?: string | null; + /** + * Homepage + */ + homepage?: string | null; + /** + * Installable + * + * whether Lens can read it (why_not says why when it can't) + */ + installable: boolean; + /** + * Why Not + */ + why_not?: string | null; + /** + * Installed + * + * your extension installed from it, if you installed it + */ + installed?: number | null; + [key: string]: unknown; +}; + +/** + * MarketplaceSuggestion + */ +export type MarketplaceSuggestion = { + /** + * Name + */ + name: string; + /** + * Title + */ + title: string; + /** + * Source + */ + source: string; + [key: string]: unknown; +}; + /** * MatterbridgeRoom */ @@ -28811,6 +29055,60 @@ export type CheckManifestResponses = { export type CheckManifestResponse = CheckManifestResponses[keyof CheckManifestResponses]; +export type ImportLinkData = { + body: ExtensionImportLink; + path?: never; + query?: never; + url: "/api/v1/extensions/import"; +}; + +export type ImportLinkErrors = { + /** + * Validation Error + */ + 422: HttpValidationError; +}; + +export type ImportLinkError = ImportLinkErrors[keyof ImportLinkErrors]; + +export type ImportLinkResponses = { + /** + * Response Extensions-Import Link + * + * Successful Response + */ + 200: ExtensionImportPreview | ExtensionImported; +}; + +export type ImportLinkResponse = ImportLinkResponses[keyof ImportLinkResponses]; + +export type ImportArchiveData = { + body: ExtensionImportArchive; + path?: never; + query?: never; + url: "/api/v1/extensions/import/archive"; +}; + +export type ImportArchiveErrors = { + /** + * Validation Error + */ + 422: HttpValidationError; +}; + +export type ImportArchiveError = ImportArchiveErrors[keyof ImportArchiveErrors]; + +export type ImportArchiveResponses = { + /** + * Response Extensions-Import Archive + * + * Successful Response + */ + 200: ExtensionImportPreview | ExtensionImported; +}; + +export type ImportArchiveResponse = ImportArchiveResponses[keyof ImportArchiveResponses]; + export type DeleteExtensionData = { body?: never; path: { @@ -28966,6 +29264,229 @@ export type TestExtensionResponses = { export type TestExtensionResponse = TestExtensionResponses[keyof TestExtensionResponses]; +export type ListMarketplacesData = { + body?: never; + path?: never; + query?: never; + url: "/api/v1/marketplaces"; +}; + +export type ListMarketplacesResponses = { + /** + * Response Extensions-List Marketplaces + * + * Successful Response + */ + 200: Array; +}; + +export type ListMarketplacesResponse = ListMarketplacesResponses[keyof ListMarketplacesResponses]; + +export type AddMarketplaceData = { + body: MarketplaceCreate; + path?: never; + query?: never; + url: "/api/v1/marketplaces"; +}; + +export type AddMarketplaceErrors = { + /** + * Validation Error + */ + 422: HttpValidationError; +}; + +export type AddMarketplaceError = AddMarketplaceErrors[keyof AddMarketplaceErrors]; + +export type AddMarketplaceResponses = { + /** + * Successful Response + */ + 200: Created; +}; + +export type AddMarketplaceResponse = AddMarketplaceResponses[keyof AddMarketplaceResponses]; + +export type SuggestedMarketplacesData = { + body?: never; + path?: never; + query?: never; + url: "/api/v1/marketplaces/suggested"; +}; + +export type SuggestedMarketplacesResponses = { + /** + * Response Extensions-Suggested Marketplaces + * + * Successful Response + */ + 200: Array; +}; + +export type SuggestedMarketplacesResponse = SuggestedMarketplacesResponses[keyof SuggestedMarketplacesResponses]; + +export type RefreshMarketplaceData = { + body?: never; + path: { + /** + * Mid + */ + mid: number; + }; + query?: never; + url: "/api/v1/marketplaces/{mid}/refresh"; +}; + +export type RefreshMarketplaceErrors = { + /** + * Validation Error + */ + 422: HttpValidationError; +}; + +export type RefreshMarketplaceError = RefreshMarketplaceErrors[keyof RefreshMarketplaceErrors]; + +export type RefreshMarketplaceResponses = { + /** + * Successful Response + */ + 200: Ok; +}; + +export type RefreshMarketplaceResponse = RefreshMarketplaceResponses[keyof RefreshMarketplaceResponses]; + +export type DeleteMarketplaceData = { + body?: never; + path: { + /** + * Mid + */ + mid: number; + }; + query?: never; + url: "/api/v1/marketplaces/{mid}"; +}; + +export type DeleteMarketplaceErrors = { + /** + * Validation Error + */ + 422: HttpValidationError; +}; + +export type DeleteMarketplaceError = DeleteMarketplaceErrors[keyof DeleteMarketplaceErrors]; + +export type DeleteMarketplaceResponses = { + /** + * Successful Response + */ + 200: Ok; +}; + +export type DeleteMarketplaceResponse = DeleteMarketplaceResponses[keyof DeleteMarketplaceResponses]; + +export type ListMarketplacePluginsData = { + body?: never; + path: { + /** + * Mid + */ + mid: number; + }; + query?: never; + url: "/api/v1/marketplaces/{mid}/plugins"; +}; + +export type ListMarketplacePluginsErrors = { + /** + * Validation Error + */ + 422: HttpValidationError; +}; + +export type ListMarketplacePluginsError = ListMarketplacePluginsErrors[keyof ListMarketplacePluginsErrors]; + +export type ListMarketplacePluginsResponses = { + /** + * Response Extensions-List Marketplace Plugins + * + * Successful Response + */ + 200: Array; +}; + +export type ListMarketplacePluginsResponse = ListMarketplacePluginsResponses[keyof ListMarketplacePluginsResponses]; + +export type PreviewMarketplacePluginData = { + body?: never; + path: { + /** + * Mid + */ + mid: number; + /** + * Name + */ + name: string; + }; + query?: never; + url: "/api/v1/marketplaces/{mid}/plugins/{name}/preview"; +}; + +export type PreviewMarketplacePluginErrors = { + /** + * Validation Error + */ + 422: HttpValidationError; +}; + +export type PreviewMarketplacePluginError = PreviewMarketplacePluginErrors[keyof PreviewMarketplacePluginErrors]; + +export type PreviewMarketplacePluginResponses = { + /** + * Successful Response + */ + 200: ExtensionImportPreview; +}; + +export type PreviewMarketplacePluginResponse = + PreviewMarketplacePluginResponses[keyof PreviewMarketplacePluginResponses]; + +export type InstallMarketplacePluginData = { + body?: never; + path: { + /** + * Mid + */ + mid: number; + /** + * Name + */ + name: string; + }; + query?: never; + url: "/api/v1/marketplaces/{mid}/plugins/{name}/install"; +}; + +export type InstallMarketplacePluginErrors = { + /** + * Validation Error + */ + 422: HttpValidationError; +}; + +export type InstallMarketplacePluginError = InstallMarketplacePluginErrors[keyof InstallMarketplacePluginErrors]; + +export type InstallMarketplacePluginResponses = { + /** + * Successful Response + */ + 200: ExtensionImported; +}; + +export type InstallMarketplacePluginResponse = + InstallMarketplacePluginResponses[keyof InstallMarketplacePluginResponses]; + export type ListChatsData = { body?: never; path?: never; diff --git a/nextjs-frontend/components/extensions/extensions-page.tsx b/nextjs-frontend/components/extensions/extensions-page.tsx index 7bb7288ef..ef2450d2b 100644 --- a/nextjs-frontend/components/extensions/extensions-page.tsx +++ b/nextjs-frontend/components/extensions/extensions-page.tsx @@ -1,7 +1,7 @@ "use client"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; -import { Blocks, MessagesSquare, Mic, Plus, Workflow } from "lucide-react"; +import { Blocks, MessagesSquare, Mic, Plus, Store, Workflow } from "lucide-react"; import Link from "next/link"; import { Extensions } from "@/app/openapi-client"; @@ -19,7 +19,12 @@ import { relative } from "@/lib/format"; export const KIND_LABEL: Record = { tool: "Tool", skill: "Skill", hook: "Hook", plugin: "Plugin" }; // the starters on a new extension: one per kind, and a Python tool (admins only) export const STARTER_LABEL: Record = { ...KIND_LABEL, python: "Python tool" }; -export const ORIGIN_LABEL: Record = { code: "code", canvas: "the canvas", chat: "chat" }; +export const ORIGIN_LABEL: Record = { + code: "code", + canvas: "the canvas", + chat: "chat", + import: "an import", +}; /** What a tool's body is, in a word. */ export function bodyLabel(e: Pick): string | null { @@ -67,6 +72,12 @@ export function ExtensionsPage() { Ask the assistant + + + + } + > + {preview.isLoading ? ( + + ) : preview.error ? ( +

+ {(preview.error as Error).message} +

+ ) : m ? ( +
+ {m.description &&

{m.description}

} +
+

What it adds to the assistant

+
    + {(m.items ?? []).map((it) => ( +
  • + {ITEM_LABEL[it.kind] ?? it.kind} + + {it.name} + {it.description && {it.description}} + +
  • + ))} +
+
+ {preview.data?.skipped?.length ? ( +
+

Left out

+
    + {preview.data.skipped.map((s, i) => ( +
  • + {s.name}: {s.why} +
  • + ))} +
+
+ ) : null} +
+ ) : null} + + ); +} + +/** A marketplace's plugins, searchable, each installable (or saying why not). */ +function Plugins({ market, onInstall }: { market: Marketplace; onInstall: (name: string) => void }) { + const client = useApiClient(); + const [q, setQ] = useState(""); + const list = useQuery({ + queryKey: ["marketplace-plugins", market.id], + queryFn: () => data(Extensions.listMarketplacePlugins({ client, path: { mid: market.id } })), + }); + const shown = useMemo(() => { + const words = q.trim().toLowerCase(); + return (list.data ?? []).filter( + (p: MarketplacePlugin) => + !words || + `${p.name} ${p.description ?? ""} ${p.category ?? ""} ${p.author ?? ""}`.toLowerCase().includes(words), + ); + }, [list.data, q]); + if (list.isLoading) return ; + if (list.error) + return ( +

+ {(list.error as Error).message} +

+ ); + return ( +
+ setQ(e.target.value)} + /> +
+ + + + + + + + + {shown.map((p) => ( + + + + + + ))} + +
PluginCategory +
+
+ + {p.name} + {p.version && v{p.version}} + {p.homepage && ( + + + + )} + + {p.description && {p.description}} + {p.author && by {p.author}} +
+
{p.category} + {p.installable ? ( + + {p.installed ? ( + + Installed + + ) : null} + + + ) : ( + + Not supported yet + + )} +
+
+
+ ); +} + +/** Marketplaces (Anthropic's, or any in its format) and imports from a link or a file. */ +export function MarketplacePage() { + const client = useApiClient(); + const qc = useQueryClient(); + const toast = useToast(); + const { admin } = useArchive(); + const [source, setSource] = useState(""); + const [link, setLink] = useState(""); + const [open, setOpen] = useState(null); + const [pending, setPending] = useState(null); + const fileRef = useRef(null); + const markets = useQuery({ + queryKey: ["marketplaces"], + queryFn: () => data(Extensions.listMarketplaces({ client })), + }); + const suggested = useQuery({ + queryKey: ["marketplaces", "suggested"], + queryFn: () => data(Extensions.suggestedMarketplaces({ client })), + }); + const done = () => void qc.invalidateQueries({ queryKey: ["marketplaces"] }); + const add = useMutation({ + mutationFn: (v: { source: string; title?: string }) => data(Extensions.addMarketplace({ client, body: v })), + onSuccess: (out) => { + setSource(""); + setOpen(out.id); + done(); + }, + onError: (e) => toast({ tone: "red", title: "Couldn’t add it", body: (e as Error).message }), + }); + const refresh = useMutation({ + mutationFn: (mid: number) => data(Extensions.refreshMarketplace({ client, path: { mid } })), + onSuccess: () => { + done(); + void qc.invalidateQueries({ queryKey: ["marketplace-plugins"] }); + }, + onError: (e) => toast({ tone: "red", title: "Couldn’t refresh it", body: (e as Error).message }), + }); + const remove = useMutation({ + mutationFn: (mid: number) => data(Extensions.deleteMarketplace({ client, path: { mid } })), + onSuccess: done, + onError: (e) => toast({ tone: "red", title: "Couldn’t remove it", body: (e as Error).message }), + }); + const pickFile = async (f: File | undefined) => { + if (!f) return; + setPending({ kind: "file", filename: f.name, data: await base64(f) }); + if (fileRef.current) fileRef.current.value = ""; + }; + const current = (markets.data ?? []).find((m) => m.id === open) ?? markets.data?.[0]; + + return ( +
+ + Back to extensions + + } + /> + +
+

+ Import a plugin or a skill +

+
+ + {({ id, describedBy }) => ( + setLink(e.target.value)} + /> + )} + + + void pickFile(e.target.files?.[0])} + /> + +
+
+ +
+

+ Marketplaces +

+ {admin ? ( +
+
+ + {({ id, describedBy }) => ( + setSource(e.target.value)} + /> + )} + + +
+ {suggested.data?.length ? ( +
+ Suggested: + {suggested.data.map((s) => ( + + ))} +
+ ) : null} +
+ ) : null} + + {markets.isLoading ? ( + + ) : markets.data?.length ? ( + <> +
+ {markets.data.map((m) => ( + + + {admin && current?.id === m.id ? ( + <> + refresh.mutate(m.id)} + > + + + remove.mutate(m.id)}> + + + + ) : null} + + ))} +
+ {current && ( + <> +

+ {current.description ? `${current.description}. ` : ""} + + {current.source} + +

+ setPending({ kind: "plugin", market: current.id, name })} + /> + + )} + + ) : ( + } title="No marketplaces yet"> + {admin + ? "Add Anthropic’s, or your team’s own: any repository with a .claude-plugin/marketplace.json." + : "An admin adds marketplaces. Meanwhile, import a plugin or a skill from a link or a file above."} + + )} +
+ + setPending(null)} /> +
+ ); +} diff --git a/nextjs-frontend/openapi.json b/nextjs-frontend/openapi.json index 1a7363de9..68157c7ba 100644 --- a/nextjs-frontend/openapi.json +++ b/nextjs-frontend/openapi.json @@ -18994,6 +18994,106 @@ } } }, + "/api/v1/extensions/import": { + "post": { + "tags": [ + "extensions" + ], + "summary": "Import Link", + "description": "A plugin or skill in Anthropic's format from a link: a GitHub or GitLab repository or folder, or a .zip. With\n`preview`, what it would add and leave out; else installed as your own (importing it again: a new version).", + "operationId": "import_link", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ExtensionImportLink" + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "Successful Response", + "content": { + "application/json": { + "schema": { + "anyOf": [ + { + "$ref": "#/components/schemas/ExtensionImportPreview" + }, + { + "$ref": "#/components/schemas/ExtensionImported" + } + ], + "title": "Response Extensions-Import Link" + } + } + } + }, + "422": { + "description": "Validation Error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/HTTPValidationError" + } + } + } + } + } + } + }, + "/api/v1/extensions/import/archive": { + "post": { + "tags": [ + "extensions" + ], + "summary": "Import Archive", + "description": "A plugin or skill from a .zip, .skill or .tar.gz you send (as base64).", + "operationId": "import_archive", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ExtensionImportArchive" + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "Successful Response", + "content": { + "application/json": { + "schema": { + "anyOf": [ + { + "$ref": "#/components/schemas/ExtensionImportPreview" + }, + { + "$ref": "#/components/schemas/ExtensionImported" + } + ], + "title": "Response Extensions-Import Archive" + } + } + } + }, + "422": { + "description": "Validation Error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/HTTPValidationError" + } + } + } + } + } + } + }, "/api/v1/extensions/{eid}": { "get": { "tags": [ @@ -19250,6 +19350,333 @@ } } }, + "/api/v1/marketplaces": { + "get": { + "tags": [ + "extensions" + ], + "summary": "List Marketplaces", + "operationId": "list_marketplaces", + "responses": { + "200": { + "description": "Successful Response", + "content": { + "application/json": { + "schema": { + "items": { + "$ref": "#/components/schemas/Marketplace" + }, + "type": "array", + "title": "Response Extensions-List Marketplaces" + } + } + } + } + } + }, + "post": { + "tags": [ + "extensions" + ], + "summary": "Add Marketplace", + "description": "Add a marketplace by its address; its list of plugins is read now.", + "operationId": "add_marketplace", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/MarketplaceCreate" + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "Successful Response", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Created" + } + } + } + }, + "422": { + "description": "Validation Error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/HTTPValidationError" + } + } + } + } + } + } + }, + "/api/v1/marketplaces/suggested": { + "get": { + "tags": [ + "extensions" + ], + "summary": "Suggested Marketplaces", + "description": "Anthropic's marketplaces, when they aren't added yet.", + "operationId": "suggested_marketplaces", + "responses": { + "200": { + "description": "Successful Response", + "content": { + "application/json": { + "schema": { + "items": { + "$ref": "#/components/schemas/MarketplaceSuggestion" + }, + "type": "array", + "title": "Response Extensions-Suggested Marketplaces" + } + } + } + } + } + } + }, + "/api/v1/marketplaces/{mid}/refresh": { + "post": { + "tags": [ + "extensions" + ], + "summary": "Refresh Marketplace", + "description": "Read its list of plugins again.", + "operationId": "refresh_marketplace", + "parameters": [ + { + "name": "mid", + "in": "path", + "required": true, + "schema": { + "type": "integer", + "title": "Mid" + } + } + ], + "responses": { + "200": { + "description": "Successful Response", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Ok" + } + } + } + }, + "422": { + "description": "Validation Error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/HTTPValidationError" + } + } + } + } + } + } + }, + "/api/v1/marketplaces/{mid}": { + "delete": { + "tags": [ + "extensions" + ], + "summary": "Delete Marketplace", + "description": "Takes it away; what was installed from it stays.", + "operationId": "delete_marketplace", + "parameters": [ + { + "name": "mid", + "in": "path", + "required": true, + "schema": { + "type": "integer", + "title": "Mid" + } + } + ], + "responses": { + "200": { + "description": "Successful Response", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Ok" + } + } + } + }, + "422": { + "description": "Validation Error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/HTTPValidationError" + } + } + } + } + } + } + }, + "/api/v1/marketplaces/{mid}/plugins": { + "get": { + "tags": [ + "extensions" + ], + "summary": "List Marketplace Plugins", + "operationId": "list_marketplace_plugins", + "parameters": [ + { + "name": "mid", + "in": "path", + "required": true, + "schema": { + "type": "integer", + "title": "Mid" + } + } + ], + "responses": { + "200": { + "description": "Successful Response", + "content": { + "application/json": { + "schema": { + "type": "array", + "items": { + "$ref": "#/components/schemas/MarketplacePlugin" + }, + "title": "Response Extensions-List Marketplace Plugins" + } + } + } + }, + "422": { + "description": "Validation Error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/HTTPValidationError" + } + } + } + } + } + } + }, + "/api/v1/marketplaces/{mid}/plugins/{name}/preview": { + "post": { + "tags": [ + "extensions" + ], + "summary": "Preview Marketplace Plugin", + "description": "What installing it would add to the assistant, and what Lens leaves out.", + "operationId": "preview_marketplace_plugin", + "parameters": [ + { + "name": "mid", + "in": "path", + "required": true, + "schema": { + "type": "integer", + "title": "Mid" + } + }, + { + "name": "name", + "in": "path", + "required": true, + "schema": { + "type": "string", + "title": "Name" + } + } + ], + "responses": { + "200": { + "description": "Successful Response", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ExtensionImportPreview" + } + } + } + }, + "422": { + "description": "Validation Error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/HTTPValidationError" + } + } + } + } + } + } + }, + "/api/v1/marketplaces/{mid}/plugins/{name}/install": { + "post": { + "tags": [ + "extensions" + ], + "summary": "Install Marketplace Plugin", + "description": "Install it as your own plugin, private until you share it; installing it again brings its new version.", + "operationId": "install_marketplace_plugin", + "parameters": [ + { + "name": "mid", + "in": "path", + "required": true, + "schema": { + "type": "integer", + "title": "Mid" + } + }, + { + "name": "name", + "in": "path", + "required": true, + "schema": { + "type": "string", + "title": "Name" + } + } + ], + "responses": { + "200": { + "description": "Successful Response", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ExtensionImported" + } + } + } + }, + "422": { + "description": "Validation Error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/HTTPValidationError" + } + } + } + } + } + } + }, "/api/v1/chats": { "get": { "tags": [ @@ -30997,7 +31424,20 @@ } ], "title": "Origin", - "description": "how this version was made: code, canvas or chat" + "description": "how this version was made: code, canvas, chat or import" + }, + "source": { + "anyOf": [ + { + "additionalProperties": true, + "type": "object" + }, + { + "type": "null" + } + ], + "title": "Source", + "description": "where it was imported from: a marketplace's plugin, a link or a file" }, "updated_at": { "anyOf": [ @@ -31177,7 +31617,20 @@ } ], "title": "Origin", - "description": "how this version was made: code, canvas or chat" + "description": "how this version was made: code, canvas, chat or import" + }, + "source": { + "anyOf": [ + { + "additionalProperties": true, + "type": "object" + }, + { + "type": "null" + } + ], + "title": "Source", + "description": "where it was imported from: a marketplace's plugin, a link or a file" }, "updated_at": { "anyOf": [ @@ -31190,17 +31643,132 @@ ], "title": "Updated At" }, - "manifest": { + "manifest": { + "type": "string", + "title": "Manifest", + "description": "the extension as a manifest to read, change and import again" + }, + "history": { + "items": { + "$ref": "#/components/schemas/ExtensionVersionInfo" + }, + "type": "array", + "title": "History", + "default": [] + } + }, + "additionalProperties": true, + "type": "object", + "required": [ + "id", + "name", + "kind", + "enabled", + "current", + "version", + "spec", + "manifest" + ], + "title": "ExtensionDetail" + }, + "ExtensionImportArchive": { + "properties": { + "filename": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "title": "Filename" + }, + "data": { + "type": "string", + "maxLength": 28000000, + "title": "Data", + "description": "a .zip, .skill or .tar.gz with a plugin or a skill, as base64 (20 MB at most)" + }, + "preview": { + "type": "boolean", + "title": "Preview", + "default": false + } + }, + "additionalProperties": false, + "type": "object", + "required": [ + "data" + ], + "title": "ExtensionImportArchive" + }, + "ExtensionImportLink": { + "properties": { + "url": { + "type": "string", + "title": "Url", + "description": "a plugin or skill: a GitHub or GitLab repository or folder, or a .zip or .tar.gz link" + }, + "preview": { + "type": "boolean", + "title": "Preview", + "description": "say what it would install, without installing it", + "default": false + } + }, + "additionalProperties": false, + "type": "object", + "required": [ + "url" + ], + "title": "ExtensionImportLink" + }, + "ExtensionImportPreview": { + "properties": { + "manifest": { + "additionalProperties": true, + "type": "object", + "title": "Manifest", + "description": "the plugin as Lens would install it" + }, + "skipped": { + "items": { + "$ref": "#/components/schemas/ExtensionSkipped" + }, + "type": "array", + "title": "Skipped", + "default": [] + }, + "source": { "type": "string", - "title": "Manifest", - "description": "the extension as a manifest to read, change and import again" + "title": "Source" + } + }, + "additionalProperties": true, + "type": "object", + "required": [ + "manifest", + "source" + ], + "title": "ExtensionImportPreview" + }, + "ExtensionImported": { + "properties": { + "id": { + "type": "integer", + "title": "Id" }, - "history": { + "version": { + "type": "integer", + "title": "Version" + }, + "skipped": { "items": { - "$ref": "#/components/schemas/ExtensionVersionInfo" + "$ref": "#/components/schemas/ExtensionSkipped" }, "type": "array", - "title": "History", + "title": "Skipped", "default": [] } }, @@ -31208,15 +31776,34 @@ "type": "object", "required": [ "id", - "name", + "version" + ], + "title": "ExtensionImported" + }, + "ExtensionSkipped": { + "properties": { + "kind": { + "type": "string", + "title": "Kind", + "description": "what was left out: hooks, mcp, skill, command, agent or file" + }, + "name": { + "type": "string", + "title": "Name" + }, + "why": { + "type": "string", + "title": "Why" + } + }, + "additionalProperties": true, + "type": "object", + "required": [ "kind", - "enabled", - "current", - "version", - "spec", - "manifest" + "name", + "why" ], - "title": "ExtensionDetail" + "title": "ExtensionSkipped" }, "ExtensionTest": { "properties": { @@ -36294,6 +36881,230 @@ ], "title": "ManifestChecked" }, + "Marketplace": { + "properties": { + "id": { + "type": "integer", + "title": "Id" + }, + "name": { + "type": "string", + "title": "Name", + "description": "the name its marketplace.json gives it" + }, + "title": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "title": "Title" + }, + "description": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "title": "Description" + }, + "source": { + "type": "string", + "title": "Source", + "description": "where it is: a repository, or a link to its marketplace.json" + }, + "plugins": { + "type": "integer", + "title": "Plugins", + "description": "how many plugins it lists" + }, + "added_by": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "title": "Added By" + }, + "fetched_at": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "title": "Fetched At" + } + }, + "additionalProperties": true, + "type": "object", + "required": [ + "id", + "name", + "source", + "plugins" + ], + "title": "Marketplace" + }, + "MarketplaceCreate": { + "properties": { + "source": { + "type": "string", + "title": "Source", + "description": "owner/repo on GitHub, a GitHub or GitLab link, or a link to a marketplace.json" + }, + "title": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "title": "Title" + } + }, + "additionalProperties": false, + "type": "object", + "required": [ + "source" + ], + "title": "MarketplaceCreate" + }, + "MarketplacePlugin": { + "properties": { + "name": { + "type": "string", + "title": "Name" + }, + "description": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "title": "Description" + }, + "version": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "title": "Version" + }, + "category": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "title": "Category" + }, + "author": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "title": "Author" + }, + "homepage": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "title": "Homepage" + }, + "installable": { + "type": "boolean", + "title": "Installable", + "description": "whether Lens can read it (why_not says why when it can't)" + }, + "why_not": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "title": "Why Not" + }, + "installed": { + "anyOf": [ + { + "type": "integer" + }, + { + "type": "null" + } + ], + "title": "Installed", + "description": "your extension installed from it, if you installed it" + } + }, + "additionalProperties": true, + "type": "object", + "required": [ + "name", + "installable" + ], + "title": "MarketplacePlugin" + }, + "MarketplaceSuggestion": { + "properties": { + "name": { + "type": "string", + "title": "Name" + }, + "title": { + "type": "string", + "title": "Title" + }, + "source": { + "type": "string", + "title": "Source" + } + }, + "additionalProperties": true, + "type": "object", + "required": [ + "name", + "title", + "source" + ], + "title": "MarketplaceSuggestion" + }, "MatterbridgeRoom": { "properties": { "gateway": { From 8e607d30e806afcd18ade1c789aaa2d96ba10b8a Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Sat, 10 Oct 2026 20:08:37 +0000 Subject: [PATCH 2/2] feat(extensions): call the tools of MCP servers an admin adds An extension of kind mcp is a Streamable HTTP MCP server: Lens lists its tools when it's saved and the assistant calls them as __. Read-only tools (readOnlyHint) answer at once, the rest ask for approval. Only admins add servers; a plugin's network MCP servers (.mcp.json) come along when an admin installs it, stdio servers are left out. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01CsyA2Kuhsnhzz2HjkD3zED --- docs/assistant.md | 11 +- docs/mcp.md | 3 + .../app/api/v1/routes/extensions.py | 3 +- fastapi_backend/app/domain/ai_tools.py | 3 +- fastapi_backend/app/domain/extensions.py | 96 ++++++++++- fastapi_backend/app/domain/marketplace.py | 78 +++++++-- fastapi_backend/app/domain/mcp_client.py | 135 +++++++++++++++ fastapi_backend/app/schemas/extensions.py | 2 +- fastapi_backend/tests/api/test_mcp_servers.py | 161 ++++++++++++++++++ .../app/openapi-client/types.gen.ts | 6 +- .../extensions/extension-editor.tsx | 7 + .../components/extensions/extensions-page.tsx | 11 +- .../extensions/marketplace-page.tsx | 2 +- nextjs-frontend/openapi.json | 9 +- 14 files changed, 492 insertions(+), 35 deletions(-) create mode 100644 fastapi_backend/app/domain/mcp_client.py create mode 100644 fastapi_backend/tests/api/test_mcp_servers.py diff --git a/docs/assistant.md b/docs/assistant.md index 1e221afe0..23f115576 100644 --- a/docs/assistant.md +++ b/docs/assistant.md @@ -144,7 +144,12 @@ shared with namespaces (admins can share with everyone), and switched on or off only when the skill applies. - **Hook**: runs when a question arrives, before or after a tool, or after an answer, to add context, block a tool or call one. Your own hooks run in your conversations; other people's run only when an admin shared them. -- **Plugin**: a bundle of tools, skills and hooks, shared and switched off as one. +- **MCP server** (admins add them): a server that speaks the Model Context Protocol over Streamable HTTP, at the + address an admin gives (this machine and the local network included, as for the model's address), with the headers + it needs (a token). Lens lists its tools when it's saved, and the assistant calls them as `__`: one the + server marks read-only answers at once, the others ask for approval. Servers that run as a program (stdio) aren't + run. +- **Plugin**: a bundle of tools, skills, hooks and MCP servers, shared and switched off as one. Make one however suits you: @@ -188,8 +193,8 @@ An installed plugin is one Lens plugin, yours and private until you share it; in version. A skill (a folder with SKILL.md) is a skill, and the assistant reads its text files (references, examples, scripts) when the instructions point at them; nothing in them runs. A command (`commands/*.md`) is a skill the assistant follows when someone asks for it, and an agent (`agents/*.md`) is a prompt tool with the agent's instructions -as its system prompt. Claude Code's hooks run commands on the computer, so they're left out, and so are a plugin's MCP -servers for now. Repositories are read from GitHub or GitLab (or the archive link) through the same guard as web +as its system prompt. Claude Code's hooks run commands on the computer, so they're left out. A plugin's MCP servers on +the network (`.mcp.json`) come along when an admin installs it; those that run as a program are left out. Repositories are read from GitHub or GitLab (or the archive link) through the same guard as web tools: public addresses only. `ai.extensions: false` turns every extension off. diff --git a/docs/mcp.md b/docs/mcp.md index 5a8b2b154..308940508 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -6,6 +6,9 @@ and sees exactly what that person sees: their namespaces, the collections they w those open. Its tools read, except the few listed under [Tools](#tools) that suggest or write, which need a write-scope token and editor access. +It works the other way too: Lens's own assistant calls the tools of MCP servers an admin adds, as extensions +([Extending the assistant](assistant.md#extending-the-assistant-tools-skills-hooks-and-plugins)). + ## Connecting The server is at `/mcp` on the web app's address: `https://lens.example.org/mcp` (`http://localhost:3000/mcp` diff --git a/fastapi_backend/app/api/v1/routes/extensions.py b/fastapi_backend/app/api/v1/routes/extensions.py index 17ad73fad..59a34b29e 100644 --- a/fastapi_backend/app/api/v1/routes/extensions.py +++ b/fastapi_backend/app/api/v1/routes/extensions.py @@ -132,9 +132,8 @@ def test_extension(eid: int, body: ExtensionTest, user: Writer, acl: Acl, db: Db needs `confirm`.""" with _errors(): g = extensions.visible_one(db, me(user), eid) - items = g["spec"]["items"] if g["kind"] == "plugin" else [g] name = body.tool or (g["name"] if g["kind"] == "tool" else None) - t = next((it for it in items if it.get("kind") == "tool" and it["name"] == name), None) + t = next((it for it in extensions.tool_items(g) if it["name"] == name), None) if not t: raise ValueError("name one of its tools to try") if t["spec"]["effect"] == "change" and not body.confirm: diff --git a/fastapi_backend/app/domain/ai_tools.py b/fastapi_backend/app/domain/ai_tools.py index 60609f3a6..1a43a96f0 100644 --- a/fastapi_backend/app/domain/ai_tools.py +++ b/fastapi_backend/app/domain/ai_tools.py @@ -1155,8 +1155,7 @@ def run_extension(db, cfg, args, user, admin, readable, toolbox=None): me = extensions.who(user["id"], user.get("email"), admin, {s: "viewer" for s in readable}) if g.get("deleted_at") or not g.get("enabled") or not extensions.can_use(g, me): raise ValueError("that extension was switched off or removed") - items = g["spec"]["items"] if g["kind"] == "plugin" else [g] - t = next((it for it in items if it.get("kind") == "tool" and it["name"] == args["tool"]), None) + t = next((it for it in extensions.tool_items(g) if it["name"] == args["tool"]), None) if not t: raise ValueError(f"the extension has no tool {args['tool']} any more") out = extensions.run_tool(db, cfg, t["spec"], extensions.tool_args(t["spec"], args.get("args") or {}), toolbox=toolbox) diff --git a/fastapi_backend/app/domain/extensions.py b/fastapi_backend/app/domain/extensions.py index 2303dc844..3a3bcaca6 100644 --- a/fastapi_backend/app/domain/extensions.py +++ b/fastapi_backend/app/domain/extensions.py @@ -35,7 +35,7 @@ from . import activity, auth, store R = store.R -KINDS = ("tool", "skill", "hook", "plugin") +KINDS = ("tool", "skill", "hook", "plugin", "mcp") VISIBILITY = ("private", "namespace", "everyone") NAME_RX = re.compile(r"^[a-z][a-z0-9_]{1,40}$") PARAM_RX = re.compile(r"^[a-z][a-z0-9_]{0,40}$") @@ -263,6 +263,71 @@ def _check_hook(spec, me, db=None): ) +def _check_mcp(spec, me, db=None): + """An MCP server: its address, the headers it needs (a token), and its tools as it listed them (mcp_client.py).""" + if set(spec) - {"url", "headers", "tools"}: + raise ValueError("an MCP server is {url, headers, tools}") + if not me["admin"]: + raise ValueError("only admins add MCP servers") + url = _text(spec.get("url"), "the MCP server's address", 2000, required=True) + if not re.match(r"^https?://[^/?#\s]+", url): + raise ValueError("an MCP server's address starts with http:// or https:// (servers that run as a program aren't run)") + headers = spec.get("headers") or {} + if not isinstance(headers, dict) or not all(isinstance(k, str) and isinstance(v, str) for k, v in headers.items()): + raise ValueError("headers are names and text values") + tools = spec.get("tools") + if tools is None: # asked of the server: what it offers now + from . import mcp_client + + tools = mcp_client.discover({"url": url, "headers": headers}) + if tools is not None: + if not isinstance(tools, list) or len(tools) > 100: + raise ValueError("an MCP server's tools are a list of at most 100") + for t in tools: + if not isinstance(t, dict) or not isinstance(t.get("name"), str) or not isinstance(t.get("schema", {}), dict): + raise ValueError("an MCP tool is {name, description, schema, read_only}") + tools = [ + store.clean( + { + "name": t["name"][:64], + "description": _text(t.get("description"), "an MCP tool's description", 1024), + "schema": t.get("schema") or {"type": "object"}, + "read_only": bool(t.get("read_only")), + } + ) + for t in tools + ] + return store.clean({"url": url, "headers": headers or None, "tools": tools}) + + +def mcp_tools(item): + """An MCP server's tools as the assistant calls them, `__`: [{name, description, spec}]. Its own + readOnlyHint is the only thing that lets one answer without asking.""" + s, out = item["spec"], [] + for t in s.get("tools") or []: + name = re.sub(r"[^A-Za-z0-9_-]", "_", f"{item['name']}__{t['name']}")[:64] + run = store.clean({"type": "mcp", "url": s["url"], "headers": s.get("headers"), "tool": t["name"]}) + spec = { + "params": [], + "schema": t.get("schema") or {"type": "object"}, + "effect": "read" if t.get("read_only") else "change", + "run": run, + } + out.append({"kind": "tool", "name": name, "description": t.get("description"), "spec": spec}) + return out + + +def tool_items(g): + """An extension's tools, a plugin's and an MCP server's included: [{kind: tool, name, description, spec}].""" + out = [] + for it in g["spec"]["items"] if g["kind"] == "plugin" else [g]: + if it["kind"] == "tool": + out.append(it) + elif it["kind"] == "mcp": + out += mcp_tools(it) + return out + + def _check_plugin(spec, me, db=None): if set(spec) - {"items"}: raise ValueError("a plugin is {items}") @@ -271,8 +336,8 @@ def _check_plugin(spec, me, db=None): raise ValueError(f"a plugin has 1 to {MAX_ITEMS} items: tools, skills and hooks") out, names = [], set() for it in items: - if not isinstance(it, dict) or it.get("kind") not in ("tool", "skill", "hook"): - raise ValueError("a plugin's items are tools, skills and hooks, each {kind, name, description, ...}") + if not isinstance(it, dict) or it.get("kind") not in ("tool", "skill", "hook", "mcp"): + raise ValueError("a plugin's items are tools, skills, hooks and MCP servers, each {kind, name, description, ...}") m = check_manifest({**it}, me, db) if m["name"] in names: raise ValueError(f"the plugin has two items called {m['name']}") @@ -281,7 +346,7 @@ def _check_plugin(spec, me, db=None): return {"items": out} -CHECKS = {"tool": _check_tool, "skill": _check_skill, "hook": _check_hook, "plugin": _check_plugin} +CHECKS = {"tool": _check_tool, "skill": _check_skill, "hook": _check_hook, "plugin": _check_plugin, "mcp": _check_mcp} def check_spec(kind, spec, me, db=None): @@ -311,7 +376,7 @@ def check_manifest(m, me, db=None): "name": name, "kind": head["kind"], "title": _text(head["title"], "the title", 80), - "description": _text(head["description"], "the description", 1024, required=head["kind"] != "hook"), + "description": _text(head["description"], "the description", 1024, required=head["kind"] not in ("hook", "mcp")), "visibility": head["visibility"], "namespaces": head["namespaces"], "spec": check_spec(head["kind"], spec, me, db), @@ -372,6 +437,12 @@ def to_manifest(ext): body = spec.pop("instructions") elif ext["kind"] == "tool" and spec["run"]["type"] == "prompt": body = spec["run"].pop("prompt") + elif ext["kind"] == "mcp": + spec.pop("tools", None) # listed by the server again when it's saved + elif ext["kind"] == "plugin": + for it in spec["items"]: + if it["kind"] == "mcp": + it["spec"].pop("tools", None) text = yaml.dump({**head, **spec}, Dumper=_Dumper, sort_keys=False, allow_unicode=True, width=120) return f"---\n{text}---\n\n{body}\n" if body is not None else text @@ -616,7 +687,11 @@ def __init__(self, db, me): # hooks see what's said and what tools give back, so only one's own run, or those an admin shared trusted = g.get("owner") == me["id"] or _owner_admin(db, g) items = g["spec"]["items"] if g["kind"] == "plugin" else [g] + if _owner_admin(db, g): # MCP servers are reached while an admin owns them + items = [x for it in items for x in (mcp_tools(it) if it["kind"] == "mcp" else [it])] for it in items: + if it["kind"] == "mcp": + continue if it["kind"] == "hook" and not trusted: continue if it["kind"] == "tool" and it["spec"]["run"]["type"] == "python" and not _owner_admin(db, g): @@ -647,6 +722,9 @@ def tool_specs(self, can_change=True): if t["spec"]["effect"] == "change" and not can_change: continue props, req = {}, [] + if "schema" in t["spec"]: # an MCP tool: its own JSON schema + props = dict(t["spec"]["schema"].get("properties") or {}) + req = [r for r in t["spec"]["schema"].get("required") or [] if r in props] for p in t["spec"]["params"]: s = {"type": PARAM_KINDS[p["kind"]]} if p.get("description"): @@ -714,6 +792,10 @@ def fill_json(value, values): def tool_args(spec, args): """A tool's arguments, checked against its parameters, with defaults filled in.""" + if "schema" in spec: # an MCP tool: the server checks its own arguments + if not isinstance(args, dict): + raise ValueError("the tool's arguments are an object") + return dict(args) out = {} params = {p["name"]: p for p in spec["params"]} for k in args: @@ -747,6 +829,10 @@ def run_tool(db, cfg, spec, args, model=None, toolbox=None): from . import tool_nodes return {"result": tool_nodes.run(db, cfg, run["graph"], args, toolbox)} + if run["type"] == "mcp": + from . import mcp_client + + return mcp_client.call(run, args) if run["type"] == "python": from . import code_tools diff --git a/fastapi_backend/app/domain/marketplace.py b/fastapi_backend/app/domain/marketplace.py index 4661e0049..20f2b3599 100644 --- a/fastapi_backend/app/domain/marketplace.py +++ b/fastapi_backend/app/domain/marketplace.py @@ -14,8 +14,9 @@ What becomes what: a skill is a skill, with its text files (references, examples, scripts to read; nothing runs); a command is a skill the assistant follows when someone asks for it; an agent is a prompt tool with the agent's -instructions as its system prompt. Claude Code's hooks run shell commands on the computer, so they're left out, and so -are MCP servers for now; `preview` lists what's left out and why. A plugin becomes one Lens plugin, owned by the person +instructions as its system prompt. Claude Code's hooks run shell commands on the computer, so they're left out; MCP +servers on the network come along when an admin installs it (servers that run as a program don't); `preview` lists +what's left out and why. A plugin becomes one Lens plugin, owned by the person who installed it and private until they share it, and installing it again brings the new version. Only admins add marketplaces: that's the approval. People who can write extensions install from the ones added, and @@ -401,9 +402,59 @@ def _norm(p): return "" if p == "." else p -def read_plugin(found, entry=None): +def _mcp(found, meta, admin, report): + """A plugin's MCP servers (.mcp.json, or mcpServers in its manifest) as MCP server items, their tools listed now. + Servers on the network only (HTTP); only admins add them.""" + conf = meta.get("mcpServers") + if isinstance(conf, str): + conf = found.get(_norm(conf)) + elif conf is None: + conf = found.get(".mcp.json") + if isinstance(conf, bytes): + try: + conf = json.loads(conf) + except ValueError: + conf = None + if isinstance(conf, dict) and isinstance(conf.get("mcpServers"), dict): + conf = conf["mcpServers"] + if not isinstance(conf, dict) or not conf: + return [] + if not admin: + report.append( + {"kind": "mcp", "name": ", ".join(conf)[:200], "why": "only admins add MCP servers: an admin installing it brings them"} + ) + return [] + from . import mcp_client + + out = [] + for name, c in list(conf.items())[:20]: + c = c if isinstance(c, dict) else {} + kind = c.get("type") or ("stdio" if c.get("command") else "http") + if kind == "stdio" or c.get("command"): + report.append( + {"kind": "mcp", "name": name, "why": "it runs a program on the computer; Lens reaches MCP servers on the network"} + ) + continue + if kind not in ("http", "streamable-http") or not isinstance(c.get("url"), str): + report.append({"kind": "mcp", "name": name, "why": f"Lens speaks MCP over HTTP; this one uses {kind}"}) + continue + headers = {k: v for k, v in (c.get("headers") or {}).items() if isinstance(k, str) and isinstance(v, str)} + server = {"url": c["url"], "headers": headers or None} + try: + tools = mcp_client.discover(server) + except ValueError as e: + tools = [] + report.append({"kind": "mcp", "name": name, "why": f"its tools couldn't be listed ({e}); save it again once it can be reached"}) + out.append( + {"kind": "mcp", "name": slug(name), "description": f"The {name} MCP server", "spec": store.clean({**server, "tools": tools})} + ) + return out + + +def read_plugin(found, entry=None, admin=False): """A plugin folder's files ({path: bytes}) as a Lens plugin manifest, and what was left out: (manifest, skipped). - `entry` is its marketplace entry, which describes it when there's no plugin.json (strict: false).""" + `entry` is its marketplace entry, which describes it when there's no plugin.json (strict: false). MCP servers come + along when an admin installs it.""" entry = dict(entry or {}) meta = {} if PLUGIN_JSON in found: @@ -458,8 +509,7 @@ def markdown(field, default): report.append( {"kind": "hooks", "name": "hooks", "why": "Claude Code's hooks run commands on the computer; Lens hooks are made in Lens"} ) - if meta.get("mcpServers") or ".mcp.json" in found: - report.append({"kind": "mcp", "name": "MCP servers", "why": "MCP servers from plugins aren't read yet"}) + items += _mcp(found, meta, admin, report) # names: each item's own, made different where two are the same seen = set() @@ -470,7 +520,9 @@ def markdown(field, default): it["name"], n = base[: 41 - len(suffix)] + suffix, n + 1 seen.add(it["name"]) if not items: - raise ValueError("nothing in it Lens can use: no skills, commands or agents" + (f" ({report[0]['why']})" if report else "")) + raise ValueError( + "nothing in it Lens can use: no skills, commands, agents or MCP servers" + (f" ({report[0]['why']})" if report else "") + ) if len(items) > extensions.MAX_ITEMS: raise ValueError(f"it has {len(items)} skills, commands and agents; a Lens plugin has at most {extensions.MAX_ITEMS}") name = meta.get("name") or (items[0]["name"] if len(items) == 1 else "plugin") @@ -631,18 +683,18 @@ def _entry(db, mid, name): raise KeyError(name) -def _read(cfg, src, entry=None): +def _read(cfg, src, entry=None, admin=False): found = files(cfg, src) # a plugin's own plugin.json describes it, unless the marketplace says it describes it (strict: false) if entry and entry.get("strict") is not False and PLUGIN_JSON in found: entry = {k: v for k, v in entry.items() if k in ("name", "description", "version")} - return read_plugin(found, entry) + return read_plugin(found, entry, admin) def preview(db, cfg, me, mid, name): """What installing a marketplace's plugin would add to the assistant, and what it leaves out.""" _, p, src = _entry(db, mid, name) - manifest, skipped = _read(cfg, src, p) + manifest, skipped = _read(cfg, src, p, me["admin"]) extensions.check_manifest(manifest, me, db) return {"manifest": manifest, "skipped": skipped, "source": show_source(src)} @@ -659,7 +711,7 @@ def install(db, cfg, me, mid, name): """Install a marketplace's plugin as this person's own plugin (private until they share it); again, a new version. (extension id, version).""" d, p, src = _entry(db, mid, name) - manifest, _ = _read(cfg, src, p) + manifest, _ = _read(cfg, src, p, me["admin"]) source = store.clean( {"marketplace": d["id"], "plugin": p["name"], "version": str(p["version"]) if p.get("version") else None, "url": show_source(src)} ) @@ -683,7 +735,7 @@ def import_link(db, cfg, me, link, dry=False): src = parse_source(link) if src["host"] == "url": raise ValueError("that's a marketplace: an admin adds it under Marketplaces") - manifest, skipped = read_plugin(files(cfg, src)) + manifest, skipped = read_plugin(files(cfg, src), admin=me["admin"]) url = show_source(src) if dry: extensions.check_manifest(manifest, me, db) @@ -701,7 +753,7 @@ def import_archive(db, me, filename, data, dry=False): if len(raw) > MAX_UPLOAD: raise ValueError(f"the file is larger than {MAX_UPLOAD // 2**20} MB") name = (filename or "the file").strip()[:200] - manifest, skipped = read_plugin(_unpack(io.BytesIO(raw), name)) + manifest, skipped = read_plugin(_unpack(io.BytesIO(raw), name), admin=me["admin"]) if dry: extensions.check_manifest(manifest, me, db) return {"manifest": manifest, "skipped": skipped, "source": name} diff --git a/fastapi_backend/app/domain/mcp_client.py b/fastapi_backend/app/domain/mcp_client.py new file mode 100644 index 000000000..6092b3dec --- /dev/null +++ b/fastapi_backend/app/domain/mcp_client.py @@ -0,0 +1,135 @@ +"""Lens as an MCP client: the assistant calls the tools of MCP servers an admin added (extensions.py, kind `mcp`). + +A server is reached over Streamable HTTP (the MCP transport for servers on the network), and answers as JSON or as an +event stream; each use is a short session of its own: `initialize`, then `tools/list` or `tools/call`. Servers that +run as a program on this computer (stdio) aren't run. Only admins add servers, as they set the model's address, so +the address is theirs to choose: a server on this machine or the local network works. +""" + +from __future__ import annotations + +import itertools +import json +import urllib.error +import urllib.request + +from . import activity + +VERSION = "2025-06-18" +CLIENT = {"name": "lens", "version": "1"} +MAX_REPLY = 2_000_000 +MAX_TOOLS = 100 +TIMEOUT = 60 +_ids = itertools.count(1) + + +class McpError(ValueError): + pass + + +def _events(raw): + """The JSON messages of an event stream's `data:` lines.""" + data = [] + for line in raw.splitlines() + [""]: + if line.startswith("data:"): + data.append(line[5:].lstrip()) + elif not line.strip() and data: + try: + yield json.loads("\n".join(data)) + except ValueError: + pass + data = [] + + +def _post(url, headers, body, timeout=TIMEOUT): + """One POST to the server: (status, response headers, body text). Tests replace it.""" + req = urllib.request.Request(url, data=json.dumps(body).encode(), method="POST", headers=headers) + try: + with urllib.request.urlopen(req, timeout=timeout) as r: # noqa: S310 (an admin's address, http or https) + return r.status, dict(r.headers.items()), r.read(MAX_REPLY).decode("utf-8", "replace") + except urllib.error.HTTPError as e: + return e.code, dict(e.headers.items()), e.read(MAX_REPLY).decode("utf-8", "replace") + except (urllib.error.URLError, OSError) as e: + raise McpError(f"couldn't reach the MCP server: {getattr(e, 'reason', e)}") from None + + +class Session: + def __init__(self, server): + if not str(server.get("url") or "").startswith(("http://", "https://")): + raise McpError("an MCP server's address starts with http:// or https://") + self.url = server["url"] + self.headers = { + "Content-Type": "application/json", + "Accept": "application/json, text/event-stream", + "User-Agent": "Lens", + **(server.get("headers") or {}), + } + self._send("initialize", {"protocolVersion": VERSION, "capabilities": {}, "clientInfo": CLIENT}) + self._send("notifications/initialized", None, note=True) + + def _send(self, method, params, note=False): + body = {"jsonrpc": "2.0", "method": method, **({"params": params} if params is not None else {})} + if not note: + body["id"] = next(_ids) + status, headers, text = _post(self.url, self.headers, body) + low = {k.lower(): v for k, v in headers.items()} + if low.get("mcp-session-id"): + self.headers["Mcp-Session-Id"] = low["mcp-session-id"] + if method == "initialize": + self.headers["MCP-Protocol-Version"] = VERSION + if note: + return None + if status >= 400: + hint = ": does it need a token in its headers?" if status in (401, 403) else "" + raise McpError(f"the MCP server answered {status}{hint}") + msgs = list(_events(text)) if "text/event-stream" in low.get("content-type", "") else [json.loads(text or "null")] + for m in msgs: + if isinstance(m, dict) and m.get("id") == body["id"]: + if m.get("error"): + raise McpError(f"the MCP server said: {(m['error'] or {}).get('message') or m['error']}") + return m.get("result") or {} + raise McpError("the MCP server didn't answer") + + def tools(self): + out, cursor = [], None + for _ in range(20): + got = self._send("tools/list", {"cursor": cursor} if cursor else {}) + out += [t for t in got.get("tools") or [] if isinstance(t, dict) and isinstance(t.get("name"), str)] + cursor = got.get("nextCursor") + if not cursor or len(out) >= MAX_TOOLS: + break + return out[:MAX_TOOLS] + + def call(self, name, args): + return self._send("tools/call", {"name": name, "arguments": args}) + + +def discover(server): + """A server's tools, as an MCP extension keeps them: {name, description, schema, read_only}.""" + with activity.call("mcp.list", detail={"url": server.get("url")}): + tools = Session(server).tools() + out = [] + for t in tools: + schema = t.get("inputSchema") if isinstance(t.get("inputSchema"), dict) else {"type": "object"} + out.append( + { + "name": t["name"][:64], + "description": str(t.get("description") or t.get("title") or t["name"])[:1024], + "schema": schema, + "read_only": bool((t.get("annotations") or {}).get("readOnlyHint")), + } + ) + return out + + +def call(run, args): + """Call one of a server's tools: its text, and anything structured it gave back.""" + with activity.call("mcp.call", detail={"url": run["url"], "tool": run["tool"]}): + got = Session(run).call(run["tool"], args) + texts = [c.get("text") for c in got.get("content") or [] if isinstance(c, dict) and c.get("type") == "text"] + out = {"text": "\n".join(t for t in texts if t)} + if got.get("structuredContent") is not None: + out["json"] = got["structuredContent"] + if got.get("isError"): + out["error"] = True + return out diff --git a/fastapi_backend/app/schemas/extensions.py b/fastapi_backend/app/schemas/extensions.py index f8391af3f..a55f05be2 100644 --- a/fastapi_backend/app/schemas/extensions.py +++ b/fastapi_backend/app/schemas/extensions.py @@ -8,7 +8,7 @@ from app.schemas.common import RequestModel, ResponseModel -Kind = Literal["tool", "skill", "hook", "plugin"] +Kind = Literal["tool", "skill", "hook", "plugin", "mcp"] Visibility = Literal["private", "namespace", "everyone"] Origin = Literal["code", "canvas", "chat"] diff --git a/fastapi_backend/tests/api/test_mcp_servers.py b/fastapi_backend/tests/api/test_mcp_servers.py new file mode 100644 index 000000000..cfd60eb18 --- /dev/null +++ b/fastapi_backend/tests/api/test_mcp_servers.py @@ -0,0 +1,161 @@ +"""MCP servers as extensions: an admin adds one, Lens lists its tools, and the assistant calls them (a read-only one at +once, the others after a yes); a plugin's MCP servers come along when an admin installs it.""" + +from __future__ import annotations + +import json + +import pytest + +from app.domain import extensions, marketplace, mcp_client +from tests import fake_llm +from tests.api._assist import Assist, sse, start_llm +from tests.api.test_marketplaces import tarball + +SERVER = """ +name: tracker +kind: mcp +description: The issue tracker +url: https://mcp.example.com/mcp +headers: {Authorization: Bearer t0ken} +""" + +TOOLS = [ + { + "name": "search", + "description": "Search issues", + "inputSchema": {"type": "object", "properties": {"q": {"type": "string"}}, "required": ["q"]}, + "annotations": {"readOnlyHint": True}, + }, + { + "name": "create-issue", + "description": "File an issue", + "inputSchema": {"type": "object", "properties": {"title": {"type": "string"}}}, + }, +] + + +@pytest.fixture +def server(monkeypatch): + """A Streamable HTTP MCP server, as far as these tests go: every message it got, with its headers.""" + got = [] + + def post(url, headers, body, timeout=60): + got.append((url, dict(headers), body)) + if headers.get("Authorization") != "Bearer t0ken": + return 401, {}, "" + m = body["method"] + if "id" not in body: + return 202, {}, "" + if m == "initialize": + result = {"protocolVersion": "2025-06-18", "capabilities": {"tools": {}}, "serverInfo": {"name": "t"}} + return ( + 200, + {"Content-Type": "application/json", "Mcp-Session-Id": "s1"}, + json.dumps({"jsonrpc": "2.0", "id": body["id"], "result": result}), + ) + assert headers.get("Mcp-Session-Id") == "s1" + if m == "tools/list": + msg = {"jsonrpc": "2.0", "id": body["id"], "result": {"tools": TOOLS}} + return 200, {"Content-Type": "text/event-stream"}, f"event: message\ndata: {json.dumps(msg)}\n\n" + if m == "tools/call": + a = body["params"] + text = f"{a['name']}: {json.dumps(a['arguments'])}" + return ( + 200, + {"Content-Type": "application/json"}, + json.dumps({"jsonrpc": "2.0", "id": body["id"], "result": {"content": [{"type": "text", "text": text}]}}), + ) + return ( + 200, + {"Content-Type": "application/json"}, + json.dumps({"jsonrpc": "2.0", "id": body["id"], "error": {"code": -32601, "message": "no"}}), + ) + + monkeypatch.setattr(mcp_client, "_post", post) + return got + + +@pytest.fixture +def llm(cfg): + srv = start_llm(cfg) + yield fake_llm.Handler + srv.shutdown() + + +@pytest.fixture +def app(cfg, db, llm): + from app.main import create_app + + return create_app(cfg, db, background=False) + + +def call(i, name, args): + return {"id": f"c{i}", "type": "function", "function": {"name": name, "arguments": json.dumps(args)}} + + +def test_an_admin_adds_a_server_and_the_assistant_uses_it(app, db, cfg, folder, new_client, server, llm): + s = Assist(app, db, cfg, folder, new_client) + c, h = s.cl["editor"] + ca, ha = s.cl["admin"] + no = c.post("/api/v1/extensions", headers=h, json={"text": SERVER}) + assert no.status_code == 400 and "only admins" in no.json()["detail"] + bad = ca.post("/api/v1/extensions", headers=ha, json={"text": SERVER.replace("t0ken", "wrong")}) + assert bad.status_code == 400 and "401" in bad.json()["detail"] + + eid = ca.post("/api/v1/extensions", headers=ha, json={"text": SERVER}).json()["id"] + got = ca.get(f"/api/v1/extensions/{eid}", headers=ha).json() + assert [(t["name"], t["read_only"]) for t in got["spec"]["tools"]] == [("search", True), ("create-issue", False)] + assert "tools" not in got["manifest"] and "url: https://mcp.example.com/mcp" in got["manifest"] # listed again on save + # tried from the extension, like any tool + tried = ca.post(f"/api/v1/extensions/{eid}/test", headers=ha, json={"tool": "tracker__search", "args": {"q": "x"}}).json() + assert tried["output"] == {"text": 'search: {"q": "x"}'} + ca.patch(f"/api/v1/extensions/{eid}", headers=ha, json={"visibility": "everyone"}) + + llm.seen.clear() + llm.tool_script = [ + {"content": "", "tool_calls": [call(1, "tracker__search", {"q": "login bug"})]}, + {"content": "", "tool_calls": [call(2, "tracker__create-issue", {"title": "Fix login"})]}, + {"content": "Found it and asked to file one."}, + ] + cid = c.post("/api/v1/chats", headers=h, json={}).json()["id"] + ev = sse(c.post(f"/api/v1/chats/{cid}/messages", headers=h, json={"content": "Any login bugs? File one."}).text) + first = [b for b in llm.seen if b.get("tools")][0] + search = next(t for t in first["tools"] if t["function"]["name"] == "tracker__search")["function"] + assert search["parameters"]["properties"] == {"q": {"type": "string"}} and search["parameters"]["required"] == ["q"] + assert ev["step"][0]["summary"] == 'Ran tracker__search(q="login bug")' + calls = [b["params"] for _, _, b in server if b["method"] == "tools/call"] + assert calls[-1] == {"name": "search", "arguments": {"q": "login bug"}} + # one that changes something waits for a yes + assert len([b for _, _, b in server if b["method"] == "tools/call"]) == 2 # the test call and the search + assert ev["step"][1]["summary"] == 'Asked for approval: Run tracker__create-issue(title="Fix login")' + + +def test_a_plugins_mcp_servers_come_with_an_admin_install(app, db, cfg, folder, new_client, server, monkeypatch): + s = Assist(app, db, cfg, folder, new_client) + c, h = s.cl["editor"] + ca, ha = s.cl["admin"] + repo = { + ".claude-plugin/plugin.json": json.dumps({"name": "tracker-kit", "description": "Issues"}), + ".mcp.json": json.dumps( + { + "mcpServers": { + "tracker": {"type": "http", "url": "https://mcp.example.com/mcp", "headers": {"Authorization": "Bearer t0ken"}}, + "local": {"command": "npx", "args": ["x"]}, + } + } + ), + } + pages = {"https://codeload.github.com/acme/kit/tar.gz/HEAD": tarball(repo)} + monkeypatch.setattr(marketplace, "_fetch", lambda cfg, url, out, limit: out.write(pages[url])) + marketplace._cache.clear() + + pre = ca.post("/api/v1/extensions/import", headers=ha, json={"url": "acme/kit", "preview": True}).json() + assert [(i["kind"], i["name"]) for i in pre["manifest"]["items"]] == [("mcp", "tracker")] + assert [x["name"] for x in pre["skipped"]] == ["local"] + eid = ca.post("/api/v1/extensions/import", headers=ha, json={"url": "acme/kit"}).json()["id"] + g = extensions.get(db, eid) + assert [t["name"] for t in extensions.tool_items(g)] == ["tracker__search", "tracker__create-issue"] + # someone else: the servers are left out, and with nothing else in it, nothing to install + no = c.post("/api/v1/extensions/import", headers=h, json={"url": "acme/kit", "preview": True}) + assert no.status_code == 400 and "only admins add MCP servers" in no.json()["detail"] diff --git a/nextjs-frontend/app/openapi-client/types.gen.ts b/nextjs-frontend/app/openapi-client/types.gen.ts index e594abbfe..67bfb7e7a 100644 --- a/nextjs-frontend/app/openapi-client/types.gen.ts +++ b/nextjs-frontend/app/openapi-client/types.gen.ts @@ -3926,7 +3926,7 @@ export type Extension = { /** * Kind */ - kind: "tool" | "skill" | "hook" | "plugin"; + kind: "tool" | "skill" | "hook" | "plugin" | "mcp"; /** * Title */ @@ -4041,7 +4041,7 @@ export type ExtensionDetail = { /** * Kind */ - kind: "tool" | "skill" | "hook" | "plugin"; + kind: "tool" | "skill" | "hook" | "plugin" | "mcp"; /** * Title */ @@ -28980,7 +28980,7 @@ export type ListExtensionsData = { /** * Kind */ - kind?: "tool" | "skill" | "hook" | "plugin" | null; + kind?: "tool" | "skill" | "hook" | "plugin" | "mcp" | null; }; url: "/api/v1/extensions"; }; diff --git a/nextjs-frontend/components/extensions/extension-editor.tsx b/nextjs-frontend/components/extensions/extension-editor.tsx index b2db3e53f..61de92c2e 100644 --- a/nextjs-frontend/components/extensions/extension-editor.tsx +++ b/nextjs-frontend/components/extensions/extension-editor.tsx @@ -77,6 +77,13 @@ run: code: | def run(text): return {"words": len(text.split()), "lines": len(text.splitlines())} +`, + mcp: `name: tracker +kind: mcp +description: Our issue tracker's MCP server (Streamable HTTP); its tools are listed when it's saved. +url: https://mcp.example.com/mcp +headers: + Authorization: Bearer your-token `, }; diff --git a/nextjs-frontend/components/extensions/extensions-page.tsx b/nextjs-frontend/components/extensions/extensions-page.tsx index ef2450d2b..a27885869 100644 --- a/nextjs-frontend/components/extensions/extensions-page.tsx +++ b/nextjs-frontend/components/extensions/extensions-page.tsx @@ -16,7 +16,13 @@ import { VISIBILITY } from "@/components/workflows/save-custom-dialog"; import { data, useApiClient } from "@/lib/api/browser"; import { relative } from "@/lib/format"; -export const KIND_LABEL: Record = { tool: "Tool", skill: "Skill", hook: "Hook", plugin: "Plugin" }; +export const KIND_LABEL: Record = { + tool: "Tool", + skill: "Skill", + hook: "Hook", + plugin: "Plugin", + mcp: "MCP server", +}; // the starters on a new extension: one per kind, and a Python tool (admins only) export const STARTER_LABEL: Record = { ...KIND_LABEL, python: "Python tool" }; export const ORIGIN_LABEL: Record = { @@ -29,6 +35,7 @@ export const ORIGIN_LABEL: Record = { /** What a tool's body is, in a word. */ export function bodyLabel(e: Pick): string | null { if (e.kind === "plugin") return `${((e.spec.items as unknown[]) ?? []).length} items`; + if (e.kind === "mcp") return `${((e.spec.tools as unknown[]) ?? []).length} tools`; if (e.kind === "hook") return `on ${String(e.spec.event ?? "")}`.replace("_", " "); if (e.kind !== "tool") return null; const run = (e.spec.run ?? {}) as { type?: string }; @@ -57,7 +64,7 @@ export function ExtensionsPage() {