Skip to content

feat(mcp): author override templates from a program's IDL - #52

Open
92Infinitus92 wants to merge 4 commits into
feat/scenarios/override/template-authoring-supportfrom
feat/template-authoring
Open

92Infinitus92 wants to merge 4 commits into
feat/scenarios/override/template-authoring-supportfrom
feat/template-authoring

Conversation

@92Infinitus92

@92Infinitus92 92Infinitus92 commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

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_template and save_override_template take collectionYaml (the text of the YAML file) and idl (the IDL as JSON, object or text), plus overwrite. 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 run anchor idl convert first.
  • Saving registers the IDL with the existing surfnet_registerIdl (skipped when the active IDL for that address is already equal) and keeps the templates in the MCP session's registry, so get_override_templates and create_scenario see them. Nothing is written to disk; after a restart the host passes the files again.
  • The surfnet never sees a saved template, so the MCP sends its overrides in a form the surfnet applies through the registered IDL alone: the account is resolved to a pubkey (a PDA from the template's or the override's seeds and values) and helper values (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 to create_scenario and to surfnet_registerScenario sent through call_surfnet_rpc.
  • keep_alive is refused in user templates: the surfnet reads keep-alive imports only from its own templates, so it would never apply.
  • The MCP prompt author_override_template and the resource str:///template_authoring_guide serve crates/core/src/scenarios/AUTHORING.md, the guide the model follows: get the IDL from the user, read a live account jsonParsed, copy a sibling template's llm_context style, write, validate, save, and prove it with create_scenario plus surfnet_registerScenario.

No change to core, the CLI or any Cargo file apart from exporting the guide (scenarios::AUTHORING_GUIDE).

Tests: cargo test -p surfpool-mcp 31 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.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_collection doesn't reject duplicate ids inside one collection. Both the loader and save_override_template keep only the last one without saying so.
  • Overwrite is not atomic. save_override_template writes idl.json first, so if writing overrides.yaml then 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 save are 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, unlike dirs and tempfile.
  • register_builtin_template_idls now 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.

Comment thread crates/core/src/surfnet/svm.rs Outdated
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();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 address a bundled template already covers.

The function name register_builtin_template_idls is also misleading now.

Comment thread crates/core/src/scenarios/AUTHORING.md Outdated
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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread crates/core/src/scenarios/registry.rs Outdated
entry.id
));
}
let templates = collection.to_override_templates(Some(idl.clone()))?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread crates/mcp/src/surfpool/mod.rs Outdated
};
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)?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread crates/mcp/src/surfpool/mod.rs Outdated
template.id
));
}
if registry.contains(&template.id) && !replaced_ids.contains(&template.id) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread crates/mcp/Cargo.toml
bs58 = { workspace = true }
crossbeam-channel = { workspace = true }
flate2 = "1.1.9"
hiro-system-kit = { workspace = true }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread crates/core/src/scenarios/registry.rs Outdated
})
}

fn subdirectories(path: &Path) -> Vec<PathBuf> {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread crates/mcp/src/surfpool/idl.rs Outdated
@@ -0,0 +1,197 @@
//! Finds a program's IDL for the template authoring tools: the IDL the surfnet has registered,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that is to be provided as a parameter, but not for us to try to find it

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants