MCP Clients
Connect to external MCP servers to consume tools, resources, and prompts from other AI applications.
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
Documentation Search
Integration with AI Chat
Resource Caching
MCP Server Endpoints
The MCP client expects servers to implement these endpoints:
/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 📊
MCPClient now tracks internal usage and performance metrics via a MCPClientStats instance using atomic variables for thread safety.
Getting Stats
Stats API
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— coverslistTools+callToolresource— coverslistResources+getResourceprompt— coverslistPrompts+getPromptdiscovery— coversgetCapabilities
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:
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
Related Documentation
External Resources
Last updated