Repository navigation
feat(mcp): author override templates from a program's IDL - #52
92Infinitus92 wants to merge 4 commits into
Conversation
There was a problem hiding this comment.
Summary
This PR adds user-written override templates. They live under ~/.surfpool/templates (or SURFPOOL_TEMPLATES_DIR) and are checked by a single validator (parse_user_collection). The surfnet, the Studio API and the MCP server all load them. Three new MCP tools are added: fetch_program_idl, validate_override_template and save_override_template. There is also an authoring prompt and a resource.
The design is careful. Bundled ids always win, a bad file is skipped rather than causing a panic, folder_name blocks path traversal, no lock is held across an await, and the tests are good.
Main concern
register_builtin_template_idls now also registers user IDLs. If a user's idl.json targets a program that already has a bundled IDL but differs from it (for example, saved via idlPath or a different on-chain version), the template that sorts last in the BTreeMap wins at every start. Because user ids usually sort after bundled ones, this silently replaces the IDL the bundled templates were checked against. That contradicts AUTHORING.md ("A later start registers the bundled IDL again"). Two user collections for the same program with different IDLs will also both register on every start, and the persistent store keeps growing.
Smaller points
parse_user_collectiondoesn't reject duplicate ids inside one collection. Both the loader andsave_override_templatekeep only the last one without saying so.- Overwrite is not atomic.
save_override_templatewritesidl.jsonfirst, so if writingoverrides.yamlthen fails, the old YAML sits next to the new IDL. Writing to a temp folder and renaming would avoid this. - Validation and the registry update in
saveare separate lock scopes. Two concurrent saves with the same id can both pass the id check (a minor TOCTOU). flate2 = "1.1.9"is pinned in the crate, not added as a workspace dependency, unlikedirsandtempfile.register_builtin_template_idlsnow covers user templates too, so the name and comment are misleading..github/workflows/claude-review.yml(a separate commit) has nothing to do with this feature and probably belongs in its own PR.
On the open questions
A global directory seems right, since the IDLs are per program rather than per project. A project-local override could reuse SURFPOOL_TEMPLATES_DIR. An apply option on create_scenario would remove a step models often get wrong.
| let registry = TemplateRegistry::new(); | ||
| let templates = registry.templates.into_values(); | ||
| for template in templates.chain(registry.keep_alive_templates.into_values()) { | ||
| let registry = template_registry(); |
There was a problem hiding this comment.
template_registry() now includes user templates, so any user idl.json that differs from the bundled IDL for the same program gets registered here. Templates are visited in id order, so whichever one sorts last becomes the active IDL at every start. A user collection saved with idlPath for Kamino (my-kamino-* sorts after kamino-*) would replace the IDL the bundled Kamino templates depend on. That contradicts AUTHORING.md line 19. Two options:
- register user IDLs only for programs that have no bundled IDL, or
- register bundled IDLs last, or skip user IDLs whose
addressa bundled template already covers.
The function name register_builtin_template_idls is also misleading now.
| reads the program's on-chain Anchor IDL account and registers it, and returns the account types | ||
| with their top-level fields. If the program publishes no Anchor IDL account, or you want a | ||
| specific IDL, pass the file as `idlPath`: it replaces the surfnet's IDL for that program for | ||
| this session. A later start registers the bundled IDL again; pass `idlPath` again to switch. |
There was a problem hiding this comment.
This isn't what happens: on the next start, register_builtin_template_idls also registers the saved user idl.json, which can win over the bundled IDL (see the comment on svm.rs). Either change the loader or correct this sentence.
| entry.id | ||
| )); | ||
| } | ||
| let templates = collection.to_override_templates(Some(idl.clone()))?; |
There was a problem hiding this comment.
Duplicate ids within one collection aren't rejected. Both load_user_templates and save_override_template insert by id, so the later entry silently replaces the earlier one. The validator should report it, e.g. by collecting ids into a HashSet and returning an error on the first repeat.
| }; | ||
| std::fs::create_dir_all(&folder).map_err(|e| format!("{}: {e}", folder.display()))?; | ||
| // The IDL goes first: a folder with a YAML and no IDL fails to load on every start. | ||
| write("idl.json", &idl.json)?; |
There was a problem hiding this comment.
When overwrite: true is set, a failure between these two writes leaves the new idl.json next to the old overrides.yaml. The pair may then fail validation on the next start, and the whole collection gets skipped. Writing both files into a sibling temp dir and then rename-ing it into place would make the replace atomic.
| template.id | ||
| )); | ||
| } | ||
| if registry.contains(&template.id) && !replaced_ids.contains(&template.id) { |
There was a problem hiding this comment.
This id check runs under a read lock that is released before save_override_template takes the write lock. Two concurrent saves of the same id into different folders can both pass, and the second one silently takes over the id in the session registry. Re-checking under the write lock in save would close this gap.
| bs58 = { workspace = true } | ||
| crossbeam-channel = { workspace = true } | ||
| flate2 = "1.1.9" | ||
| hiro-system-kit = { workspace = true } |
There was a problem hiding this comment.
Nit: other deps use { workspace = true }. Consider adding flate2 to the workspace Cargo.toml (zip already depends on flate2) rather than pinning a version here.
| }) | ||
| } | ||
|
|
||
| fn subdirectories(path: &Path) -> Vec<PathBuf> { |
There was a problem hiding this comment.
Can we ask the user simply to upload the yaml or the file directly, so we get it here from the front-end. I don't think surfpool should read user's file system
| @@ -0,0 +1,197 @@ | |||
| //! Finds a program's IDL for the template authoring tools: the IDL the surfnet has registered, | |||
There was a problem hiding this comment.
I believe that is to be provided as a parameter, but not for us to try to find it
Lets a model author override templates for a program with an IDL from any MCP host (Claude Code, Codex, Cursor), and play scenarios with them in the same MCP session, without a rebuild. Milestone 3.8 of the LimeChain engagement.
The MCP only. Surfpool reads nothing from the user's file system and looks nothing up on chain: the host reads the user's files and passes their contents.
validate_override_templateandsave_override_templatetakecollectionYaml(the text of the YAML file) andidl(the IDL as JSON, object or text), plusoverwrite. Validation checks the YAML against the IDL: account type, every property path, duplicate ids in the collection, and ids that a built-in or keep-alive template already uses. A legacy (pre-0.30) IDL is refused with a hint to runanchor idl convertfirst.surfnet_registerIdl(skipped when the active IDL for that address is already equal) and keeps the templates in the MCP session's registry, soget_override_templatesandcreate_scenariosee them. Nothing is written to disk; after a restart the host passes the files again.input,constant_ref,dynamic_ref, PDA seed references) are dropped. A missing seed value is refused before anything is sent. Built-in templates' overrides are sent unchanged. This applies tocreate_scenarioand tosurfnet_registerScenariosent throughcall_surfnet_rpc.keep_aliveis refused in user templates: the surfnet reads keep-alive imports only from its own templates, so it would never apply.author_override_templateand the resourcestr:///template_authoring_guideservecrates/core/src/scenarios/AUTHORING.md, the guide the model follows: get the IDL from the user, read a live accountjsonParsed, copy a sibling template'sllm_contextstyle, write, validate, save, and prove it withcreate_scenarioplussurfnet_registerScenario.No change to core, the CLI or any Cargo file apart from exporting the guide (
scenarios::AUTHORING_GUIDE).Tests:
cargo test -p surfpool-mcp31 passed, including a collection checked against its IDL (refusals, legacy IDL, bad address, IDL as text), saving that registers the IDL against an in-process surfnet and adds the templates to the session, a saved-template override turned into a pubkey account with only field values, a missing seed refused, and a built-in override sent unchanged. Core suite 898 passed; fmt and clippy clean on the new code;cargo build -p surfpool-cli.