mcpConnect

Connects to a remote MCP server over stdio or HTTP and returns a client handle. The handshake runs at the connect call, so a bad command or URL fails where the developer is looking, rather than at the first tools() call somewhere else. The client declares no capabilities of its own, so it declines a server's sampling or elicitation request rather than leaving the server parked. A tool that reports failure throws an exception (type = "MCPToolError"); a tool with structured output returns that data directly.

mcpConnect( transport [, options] ) → returns any

This function requires RustCFML.  Not supported on Lucee, Adobe ColdFusion, etc.

Argument Reference

transport string
Required

The transport to use to reach the remote server: "stdio" launches it as a subprocess, "http" talks to it over a URL.
Values:
  • stdio
  • http

options struct

A struct of transport options. For stdio: command and args. For http: url and headers. Named arguments other than transport are also merged in as options, so both mcpConnect( "stdio", command = "..." ) and mcpConnect( "stdio", { command = "..." } ) work.

Links more information about mcpConnect

Examples
Sample code invoking the mcpConnect function

Connect to a filesystem server over stdio and call one of its tools.

client = mcpConnect(
      transport = "stdio"
    , command   = "npx -y @modelcontextprotocol/server-filesystem /tmp"
);

tools = client.tools();
rows  = client.call( "read_file", { path = "/tmp/notes.txt" } );
client.close();

Connect to a remote MCP server over HTTP with an authorization header, then list and call tools.

client = mcpConnect(
      transport = "http"
    , url       = "https://example.com/mcp"
    , headers   = { Authorization = "Bearer #token#" }
);

tools = client.tools();
result = client.call( "search", { query = "invoices" } );
client.close();

Signup for cfbreak to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.

Fork me on GitHub