Connect your app via MCP for tool-use

MCP is a protocol developed by Anthropic to enable language models to interact with existing services and other agents. A service providing an MCP interface can expose its functionality as tool-use.

MCP as a stellar API

MCP is a stateful protocol based on JSON RPC, it consists of three parts1:

  • initialisation: This advertises the client to the server
  • tool/list: this advertises the list of tools the client can use from the server
  • tool/call: Enables the client to make a tool call on the server

Given an API A, we can add MCP support for it by defining MCP endpoints for it. For an endpoint to be MCP-compatible we need to give:

  • a name
  • a description
  • a JSON schema for the request data
  • a JSON schema for the reponse data
  • an optional set of annotations
  • an optional title

There a helper type EMCP that records all this information for you, by defining a value of EMCP for each of your sub-APIs you can generate an API that interacts with JSONRPC using the function mcpServer:

mcpServer :
  (ls : List EMCP) ->
  (ne : NonEmpty ls) =>
  JSONRPC =&> Any.Maybe (AsMCP (FoldPlusF (.api) ls))

An API with three sub-APIs A1, A2, A2 will generate the API-transformer JSON =&> Any.Maybe (AsMCP (A1 + A2 + A3)) assuming you have a value of EMCP for each sub-api:

endpointA1 : EMCP
endpointA2 : EMCP
endpointA3 : EMCP

appAsMCP : JSONRPC =&> Any.Maybe (AsMCP (A1 + A2 + A3))
appAsMCP = mcpServer [endpointA1, endpointA2, endpointA3]

You will notice AsMCP, this modifies the API passed in arguments to add the extra messages that are part of the procotol, but not part of the API itself

Given an API A, AsMCP A is an alias for a choice of calling either A or MCP_INIT or a trivial endpoint End. Those additional endpoints are implemented automatically for you via the mcpServer function. MCP_INIT handle the initialisation protocol returning information about the server, and End handles tool/list and notification/initalized messages.

JSONRPC
JSONRPC
App
App
App
...
RPC router
RPC router
HTTP
HTTP
RPC router
RPC router
CLI
CLI
Text is not SVG - cannot display
Chose either HTTP or CLI substrates to run your MCP server.

To run your MCP Server via HTTP you need to depends on the stellar-http package and import Stellar.HTTP, Stellar.HTTP.DSL and use the JSONHTTP and JSONRPC combinators.

To run your MCP server via CLI you need to import Stellar.CLI, Stellar.JSONRPC and use the JSONRPC combinators.

Future work

Stellar does not handle the stateful nature of MCP, this is planned and we should be able to track client IDs automatically as well as keep track of the state of each connected client in the protocol using the sequence product &>.

We only support MCP via HTTP and only with one-shot connections, we do not honor the keep-alive header. This is an artefact of the Idris network library which we plan to address but was not necessary to instanciate the proof of concept.

MCP specs out a notification system that we do not support for similar reasons, the Idris networking library does not have streaming capabilities at this time. We plan to remedy this, although, because this improvement does not leverage the unique capabilities of stellar, it’s been postponed.

  1. In addition to those three endpoints, MCP servers can notify clients of different tool changes, stellar currently does not support this feature