🔌MCP Client - Model Context Protocol

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

// Create an MCP client
client = MCP( "http://localhost:3000" )

// Send a request to a tool
result = client.send( "searchDocs", {
    query: "BoxLang syntax",
    limit: 10
} )

// Check the response
if ( result.getSuccess() ) {
    writeOutput( result.getData() )
} else {
    writeOutput( "Error: " & result.getError() )
}

⚙️ 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

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