Skip to main content

Actions

The ballerina/mcp package exposes the following clients:

ClientPurpose
Streamable Http ClientConnects to an MCP server over Streamable HTTP transport to discover and invoke tools.

For event-driven integration, see the Trigger Reference.


Streamable HTTP client

Connects to an MCP server over Streamable HTTP transport to discover and invoke tools.

Configuration

FieldTypeDefaultDescription
serverUrlstringRequiredThe URL of the MCP server endpoint (e.g., http://localhost:9090/mcp).
sessionIdstring()Optional session ID for resuming a previous session.
timeoutdecimal60HTTP request timeout in seconds.
retryConfighttp:RetryConfig()Retry configuration for failed HTTP requests.
secureSockethttp:ClientSecureSocket()SSL/TLS configuration for secure connections.
proxyhttp:ProxyConfig()Proxy server configuration.

Initializing the client

import ballerina/mcp;

configurable string serverUrl = ?;

mcp:StreamableHttpClient mcpClient = check new (serverUrl);

Operations

Connection management

initialize

Initializes the MCP connection by performing the protocol handshake and capability exchange with the server.

Parameters:

NameTypeRequiredDescription
clientInfoImplementationNoClient name and version information. Defaults to {name: "MCP Client", version: "1.0.0"}.
capabilitiesClientCapabilitiesNoClient capabilities to advertise to the server.
headersmap<string|string[]>NoAdditional HTTP headers for the initialization request.

Returns: ClientError?

Sample code:

check mcpClient->initialize(
{name: "Weather Client", version: "1.0.0"}
);
close

Closes the MCP session and disconnects from the server.

Returns: ClientError?

Sample code:

check mcpClient->close();

Tool discovery

listTools

Retrieves the list of available tools from the MCP server, including their names, descriptions, and input schemas.

Parameters:

NameTypeRequiredDescription
headersmap<string|string[]>NoAdditional HTTP headers for the request.

Returns: ListToolsResult|ClientError

Sample code:

mcp:ListToolsResult toolsResult = check mcpClient->listTools();

Sample response:

{
"tools": [
{
"name": "getCurrentWeather",
"description": "Get the current weather for a given city",
"inputSchema": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "The city name"
}
},
"required": ["city"]
}
},
{
"name": "getWeatherForecast",
"description": "Get weather forecast for a location",
"inputSchema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The location name"
},
"days": {
"type": "integer",
"description": "Number of forecast days"
}
},
"required": ["location", "days"]
}
}
]
}

Tool invocation

callTool

Executes a specific tool on the MCP server with the given parameters and returns the result.

Parameters:

NameTypeRequiredDescription
paramsCallToolParamsYesThe tool call parameters including the tool name and arguments map.
headersmap<string|string[]>NoAdditional HTTP headers for the request.

Returns: CallToolResult|ClientError

Sample code:

mcp:CallToolResult result = check mcpClient->callTool({
name: "getCurrentWeather",
arguments: {"city": "London"}
});

Sample response:

{
"content": [
{
"type": "text",
"text": "Current weather in London: 15°C, Partly Cloudy, Humidity: 72%, Wind: 12 km/h SW"
}
],
"isError": false
}

Streaming

subscribeToServerMessages

Opens a Server-Sent Events (SSE) stream for receiving asynchronous server-to-client messages such as notifications and progress updates.

Returns: stream<JsonRpcMessage, StreamError?>|ClientError

Sample code:

stream<mcp:JsonRpcMessage, mcp:StreamError?> messageStream =
check mcpClient->subscribeToServerMessages();

check from mcp:JsonRpcMessage message in messageStream
do {
// Process each server message
};

Sample response:

{"jsonrpc": "2.0", "method": "notifications/tools/list_changed"}