MCP server integration
The MCP integration gives model-context-protocol clients a controlled way to use Surfpool as a local Solana test environment. It lets a client start a Surfnet, prepare accounts, call Surfnet RPC methods, discover scenario data, inspect Phoenix trader state, and create focused protocol scenarios. The focused scenario helper covers Pump Graduation. See Developer integrations for the wider integration map. The Rust SDK and Node.js SDK pages describe related client paths.
Client connection model
The MCP layer supports two connection paths. The surfpool mcp command serves MCP over standard input and output for local tools such as IDE agents. The normal Surfpool service mounts a stateful MCP endpoint at /mcp for HTTP clients.
Local process connection
The local process mode is intended for an IDE or agent that launches Surfpool as a child process. The client sends MCP messages over standard input and receives tool and resource results over standard output. The repository README documents the expected release binary and project-root setup.
References: crates/…/lib.rs• L1-28 crates/…/README.md• L1-10 crates/…/mod.rs• L107-136 crates/…/mod.rs• L938-1008
HTTP connection
The HTTP mode places the MCP service under the running Surfpool web server. It uses stateful sessions and keeps the session alive with periodic events. A remote client can therefore keep using one MCP connection while it works with local Surfnet services.
References: crates/…/mod.rs• L57-109
Surfpool capability model
The Surfpool service is the MCP-facing view of Surfpool. It presents high-level tools for common setup work, a generic RPC bridge for operations that already exist in Surfnet, and scenario tools for general account overrides plus a focused Pump Graduation flow. It also advertises resources that describe the RPC surface and available scenario templates. Phoenix helpers can report current markets and a trader's positions and margin. Each service instance tracks the local networks it starts and shares a scenario template registry across its tools.
The main interaction flow moves from environment setup to state preparation and then to application-specific actions. This diagram shows how an MCP client uses the layer without needing to know the internal runtime path.
flowchart LR
Client[MCP client] --> Session[MCP session]
Session --> Start[Start Surfnet]
Start --> State[Set SOL or token state]
Session --> RPC[Call Surfnet RPC]
Session --> Discover[Read endpoint or template resources]
Discover --> Scenario[Create general or Pump scenario]
Session --> Inspect[Inspect Phoenix markets or positions]
State --> Local[Local Surfnet]
RPC --> Local
Inspect --> Local
Scenario --> Studio[Surfpool Studio]
Surfnet lifecycle
The start_surfnet tool finds a pair of free local ports and returns either a command for the client to run or a directly started background Surfnet. The run_as_subprocess choice separates agent-controlled startup from server-controlled startup. The combined start_surfnet_with_token_accounts tool always starts a background Surfnet before it prepares the requested accounts.
The server reports the Surfnet ID and RPC URL when startup succeeds. A headless Surfnet waits for the local runtime to report that it is ready before the tool returns. This prevents clients from sending setup requests to a service that has not finished starting.
References: crates/…/mod.rs• L580-614 crates/…/mod.rs• L662-738 crates/…/start_surfnet.rs• L59-193 crates/…/helpers.rs• L3-20
Account state preparation
The set_token_accounts tool prepares SOL or SPL token balances on an existing Surfnet. A request can target several owners and several tokens. An omitted owner creates a new wallet and returns its public and secret key data. This lets an agent create a usable test account without a separate wallet step.
Token symbols are resolved through the verified-token list. Callers can also provide a mint address and an optional token program. The helper treats requested amounts as human-readable units, converts them to the token's smallest units, and sends the matching account update through Surfnet RPC. SOL uses the native balance path. Other tokens use the associated token account for the selected token program.
The combined startup tool keeps successful account updates when another requested update fails. Its response reports both the accounts that were prepared and the errors that need attention.
References: crates/…/mod.rs• L53-61 crates/…/mod.rs• L616-660 crates/…/mod.rs• L662-738 crates/…/set_token_account.rs• L43-80 crates/…/set_token_account.rs• L148-227
RPC bridge
The call_surfnet_rpc tool exposes the broader Surfnet RPC surface without adding one MCP tool for every RPC method. The client supplies a local Surfnet port, method name, and JSON-like parameters. The MCP layer forwards the request and returns either the RPC result or a clear error response.
The str:///rpc_endpoints resource supplies the method catalog and parameter descriptions. Clients should read it when they need an operation that is not covered by a dedicated setup tool. Scenario creation is a separate flow and should not be sent through this generic bridge.
References: crates/…/mod.rs• L741-824 crates/…/mod.rs• L1404-1460 crates/…/rpc_endpoints.json• L1-35
Scenario authoring model
The scenario tools let an MCP client describe account state changes at selected slots and hand the result to Surfpool Studio. The client first learns the supported account shapes. It then resolves fixed choices such as feeds or markets and dynamic choices such as current Phoenix markets before it submits a scenario that follows those contracts. Dedicated Pump Graduation tools cover the common protocol state-preparation case without requiring the client to assemble the full override set.
Template discovery
Template discovery uses a light index followed by focused detail. The get_override_templates tool and the str:///override_templates resource return each template's identity, summary, protocol, account type, tags, and whether extra model context exists. They omit large properties, address rules, constant catalogs, and IDL data. A client can therefore choose a template without loading every detail.
The get_override_template tool returns the full shape for one selected template. It includes the editable properties, address rules, raw-layout write-path flag, model context, and a summary of each constant with its label, description, and option count. It does not inline the full option lists. The search_constant_options tool finds usable values by matching option IDs, labels, descriptions, or values. Results are capped at 20 and report the full match count and whether the result is truncated.
This makes discovery a staged exchange: read the index, fetch one template's detail, then search only the constant choices needed for the scenario. For a dynamic reference, the template names the tool that supplies the current value. The returned option value is the value that create_scenario expects.
References: crates/…/mod.rs• L70-119 crates/…/mod.rs• L1268-1402 crates/…/mod.rs• L1461-1493
Scenario registration
The create_scenario tool validates template IDs and property values before it sends the scenario to the Studio scenario service. For PDA-based accounts, the selected template and values determine the account address. The tool also normalizes common malformed PDA seed input so values placed directly in a seed can move into the expected values map before validation.
Constant references must use an exact value from the selected template's catalog. Dynamic references must be supplied as non-empty values resolved by their named tool. Missing values, non-string mint values, and case changes in base58 values are rejected before registration. Phoenix market symbols are checked against the current market list when that state is available. A successful request returns a Studio editor URL that a client can present to the user.
Scenario IDs act as stable names. Reusing an ID with the same scenario is accepted as a no-op. Different content under that ID is rejected as a conflict. This protects an agent from treating a failed replacement as a successful registration.
References: crates/…/mod.rs• L824-1103 crates/…/mod.rs• L489-577 crates/…/mod.rs• L175-221
Phoenix Eternal market discovery
The list_phoenix_markets tool exposes the current Phoenix Eternal perp market catalog from the target Surfnet. It returns symbols, orderbook addresses, and the price and risk values needed to choose a market or calculate a relative price change. Clients use this tool for template properties whose value depends on live Phoenix state instead of using a fixed catalog.
References: crates/…/mod.rs• L1164-1212
Phoenix Eternal trader positions
The list_phoenix_trader_positions tool reports the positions and margin view for one Phoenix Eternal Trader account. It can also project margin after selected markets move to target ticks. This gives a client a read path for checking whether a trader can cross into liquidation before it creates a market-move scenario.
The response uses Phoenix market symbols and includes each position's side, size, orderbook, and maintenance margin. It also reports whether the trader is active, current and projected margin, liquidatability, and any refusal reason. The tool reads the state used by Phoenix's position and margin view, so clients should use its result instead of decoding the Trader account themselves.
References: crates/…/mod.rs• L1214-1266 crates/…/mod.rs• L1546-1577
Pump Graduation scenario
The create_pump_graduation_scenario tool creates an editable scenario for a live Token-2022 Pump token. It checks the token address, reads the required token and Pump account state from the target Surfnet, and sends the resulting preparation to Surfpool Studio. The flow also checks the expected graduation conditions. These include an incomplete bonding curve and no canonical PumpSwap pool.
The tool accepts an optional Surfnet port and defaults to the standard local RPC port. It returns a structured error when the mint is invalid or when required account state is missing. The client can then correct the input instead of retrying the same request.
References: crates/…/mod.rs• L140-149 crates/…/mod.rs• L1103-1162 crates/…/graduation_builder.rs
Token lookup
The get_token_address tool provides a small lookup path for clients that know a verified token symbol but need its mint address. The response includes the token name and decimal scale when the symbol is known. Unknown symbols return an explicit not-found result instead of an inferred address.
References: crates/…/mod.rs• L405-442 crates/…/mod.rs• L1404-1425 crates/…/verified_tokens.rs• L48-60
Client workflow
An MCP client can use the integration as a short setup-and-act sequence. Dedicated tools cover the common path. Resources provide the data needed for the less direct path.
- Connect over standard input and output or to the HTTP
/mcpendpoint. - Start a Surfnet by executing the returned command or by asking the server to run it in the background.
- Prepare owners and token balances when the test needs known initial state.
- Read
str:///rpc_endpointsbefore selecting an RPC method that has no dedicated tool. - For a general scenario, read
str:///override_templatesor callget_override_templatesto choose a template. - Call
get_override_templatefor the selected template before building the override. - Search constant options when a template needs a feed, market, token mint, or other fixed value. Use the named dynamic tool when a property requires current state, such as
list_phoenix_markets. - Use
list_phoenix_trader_positionswhen a Phoenix test depends on current positions or projected margin. - Use
create_pump_graduation_scenariowhen the target state is the supported Pump protocol case. - Submit the scenario and use the returned Studio URL for review or further work.
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the standalone
surfpool mcpcommand and the HTTP/mcproute aligned with the sameSurfpooltool and resource contract. - Update
str:///rpc_endpointswhen Surfnet RPC names or parameter shapes change. The generic RPC tool relies on that catalog for client guidance. - Keep the template index, single-template detail, constant search results, and scenario validation in agreement when template fields or constant values change.
- Preserve the light index and summarized constant catalogs so large IDLs and option lists do not return in the first discovery response.
- Keep the dedicated Pump Graduation tool aligned with the account sets and state rules used by its scenario builder.
- Keep dynamic template references aligned with the tool named in each property's source. In particular, keep Phoenix market names consistent with the live market listing.
- Keep Phoenix trader position inputs and projected margin outputs aligned with the market symbols and tick units used by Phoenix play tools.
- Preserve the port pairing rules used by Surfnet startup so the returned RPC URL and WebSocket service remain usable together.
- Treat generated wallet key data as a client-visible security contract when account creation behavior changes.
- Keep token amount units consistent between MCP input, decimal conversion, Surfnet account updates, and the success response.
- Keep the token program used for account updates consistent with the associated token address returned to the client.
- Preserve partial-result reporting when a multi-account request contains both successful and failed updates.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Both the selected RPC port and its WebSocket partner are unavailable | Surfnet startup returns an error and no usable local endpoint | Choose the next available Surfnet port pair before starting the network |
| A headless Surfnet fails, times out, or never reaches ready state | The client receives a startup error instead of a ready RPC URL | Wait for the runtime readiness event and report startup failure |
| A token symbol is not in the verified-token list and no valid mint address is supplied | That account update fails. Other updates in the same request can still be reported | Use get_token_address or provide a mint address directly |
| A requested amount cannot be converted to the token's smallest unit | The update can use the unconverted amount and produce an unintended balance | Keep amount and decimal-scale values within the supported numeric range |
| The target Surfnet is not reachable at the supplied address | RPC or account setup returns a transport error | Start the Surfnet first and use the RPC address from its startup response |
| A client skips template discovery or uses an unknown template or invalid constant value | Scenario registration returns a validation error | Read the index, fetch the selected template detail, and resolve constants through search_constant_options |
| A dynamic reference is missing or names a Phoenix market that is not in the current catalog | Scenario validation returns an error before registration | Use the tool named by the template property, such as list_phoenix_markets, and pass its exact value |
| A Phoenix trader address is invalid, or a requested target tick is not a whole number | Position and projected-margin lookup returns an error | Pass a valid Trader account address and whole-number target ticks |
| The Phoenix PerpAssetMap account is missing from the target Surfnet | Market discovery and position lookup cannot build a current market view | Use a Surfnet that contains the Phoenix state needed by the query |
| A required Pump Graduation account is missing or the token mint is invalid | The dedicated scenario is not staged in Studio | Use a valid mint and prepare the target Surfnet state before calling the tool |
| A different scenario already uses the requested ID | Studio returns a conflict and the MCP tool does not report success | Pick a new ID or delete the existing scenario first |
Key files
| File | Why to read it |
|---|---|
| crates/…/lib.rs | Defines the standalone MCP server entry point and its standard-input/output connection. |
| crates/…/mod.rs | Defines the MCP-facing tools, resources, response contracts, general scenario flow, and focused protocol scenario flow. |
| crates/…/start_surfnet.rs | Explains the two Surfnet startup modes and readiness result. |
| crates/…/set_token_account.rs | Explains account creation, token lookup, amount conversion, program selection, and balance preparation. |
| crates/…/helpers.rs | Defines the local port-pair selection used by Surfnet startup. |
| crates/…/README.md | Gives the short setup path for IDE clients using the local MCP process. |
| crates/…/mod.rs | Connects the mcp CLI command to the MCP crate. |
| crates/…/mod.rs | Mounts MCP at /mcp and accepts scenario registrations through the Studio service. |
| crates/…/rpc_endpoints.json | Supplies the RPC method catalog exposed as an MCP resource. |
| crates/…/verified_tokens.rs | Supplies the verified symbols and mint metadata used by token tools. |
| crates/…/graduation_builder.rs | Defines the protocol state preparation used by the Pump Graduation tool. |
| crates/…/state_builder.rs | Defines the Phoenix Eternal state used by the market and trader query helpers. |