plugMCP Clients

The BoxLang AI Module includes built-in support for the Model Context Protocol (MCP), a standardized way for AI applications to connect to external data sources and tools.

🎯 What is MCP?

Model Context Protocol is an open protocol that enables AI models to:

🏗️ MCP Client Architecture

Model Context Protocol is an open protocol that enables AI models to:

  • 🔧 Access Tools: Execute functions on remote servers

  • 📚 Read Resources: Access documents, databases, and external data

  • 💬 Use Prompts: Leverage server-defined prompt templates

  • 🔍 Discover Capabilities: Dynamically learn what a server offers

🚀 Basic Usage

⚙️ Client Configuration

⏱️ Timeout Configuration

Set request timeout in milliseconds:

Custom Headers

Add custom HTTP headers to requests:

Authentication

Bearer Token Authentication:

Basic Authentication:

Callbacks

Register success and error callbacks:

Fluent Chaining

All configuration methods return this for fluent API:

Discovery Methods

List Tools

Discover available tools on the server:

List Resources

Get available resources:

List Prompts

Discover prompt templates:

Get Capabilities

Check server capabilities:

Execution Methods

Send Tool Request

Invoke a tool with arguments:

Read Resource

Access a resource by URI:

Get Prompt

Retrieve a prompt template with arguments:

Response Structure

All MCP methods return an MCPResponse object:

Error Handling

Network Errors

HTTP Errors

Try-Catch Pattern

Real-World Examples

Integration with AI Chat

Resource Caching

MCP Server Endpoints

The MCP client expects servers to implement these endpoints:

Endpoint
Method
Purpose
Request Body

/tools

GET

List available tools

None

/tools/{name}/invoke

POST

Invoke a tool

{ "params": {...} }

/resources

GET

List available resources

None

/resources/read

POST

Read a resource

{ "uri": "..." }

/prompts

GET

List available prompts

None

/prompts/{name}

POST

Get a prompt

{ "params": {...} }

/capabilities

GET

Get server capabilities

None

Stats & Observability 📊

circle-info

Since BoxLang AI v3.2.0+

MCPClient now tracks internal usage and performance metrics via a MCPClientStats instance using atomic variables for thread safety.

Getting Stats

Stats API

Method
Description

getStats()

Full stats struct with per-operation, per-tool, per-URI, per-prompt breakdowns

getSummary()

Lightweight summary with totalCalls, successRate, avgResponseTime, per-type totals, totalErrors, lastCallAt

resetStats()

Reset all counters to zero (fluent)

Stats Breakdown

Tracked operation types:

  • tool — covers listTools + callTool

  • resource — covers listResources + getResource

  • prompt — covers listPrompts + getPrompt

  • discovery — covers getCapabilities

Per-tool stats: count, totalTime, avgTime

Per-URI stats: resource read counts

Per-prompt stats: prompt generation counts

Events

Three new interception points fire from every HTTP call:

Event
When Fired

onMCPClientRequest

Before the HTTP request

onMCPClientResponse

On successful response

onMCPClientError

On HTTP errors or network exceptions

Best Practices

1. Use Configuration Objects

2. Always Check Success

3. Use Callbacks for Observability

4. Timeout Appropriately

5. Secure Credentials

Testing MCP Clients

Unit Testing

External Resources

Last updated