MCPServer

Get or create an MCP (Model Context Protocol) server instance for registering and managing tools, resources, and prompts. MCP servers expose your application's capabilities to AI clients through a standardized protocol.

Syntax

MCPServer(name, description, version, cors, statsEnabled, force)

Parameters

Parameter
Type
Required
Default
Description

name

string

No

"default"

Unique name/identifier for the server instance

description

string

No

"BoxLang AI MCP Server"

Server description for MCP capabilities response

version

string

No

"1.0.0"

Server version

cors

string

No

""

CORS allowed origin (empty = no CORS header, secure by default)

statsEnabled

boolean

No

true

Enable statistics tracking

force

boolean

No

false

Force rebuild of server instance even if exists

Returns

Returns an MCPServer instance with fluent API for:

  • Tool management: registerTool(), getTool(), removeTool(), listTools()

  • Resource management: registerResource(), getResource(), removeResource(), listResources()

  • Prompt management: registerPrompt(), getPrompt(), removePrompt(), listPrompts()

  • Request handling: handleRequest(), handleToolCall(), handleResourceGet(), handlePromptGet()

  • Configuration: setDescription(), setVersion(), setCORS(), enableStats()

  • Statistics: getStats(), getToolCount(), getResourceCount(), getPromptCount()

Examples

Basic MCP Server

Complete Server Setup

HTTP Endpoint

Register Resource

Register Prompt

Multiple Tools

CORS Configuration

Statistics Tracking

Multi-Server Architecture

Dynamic Tool Registration

Inspect Server

Force Rebuild

Singleton Pattern

Remove Tools/Resources

Error Handling

Integration with Agent

Secure Endpoint

Development vs Production

Notes

  • 🌐 Singleton: Servers are singleton by name - same instance returned for same name

  • 🔧 Global Storage: Stored globally, accessible from anywhere (Application.bx → endpoint)

  • 🎯 Protocol: Implements Model Context Protocol standard

  • 🔐 Security: No CORS by default - explicitly set allowed origins

  • 📊 Statistics: Track tool usage, call counts, and performance

  • 🚀 Performance: Efficient request handling and tool dispatch

  • 🔄 Hot Reload: Use force: true to rebuild during development

  • MCP() - Create MCP client to consume servers

  • aiTool() - Create tools to register with server

  • aiAgent() - Use MCP tools with agents

Best Practices

Register at startup - Register tools in Application.bx onApplicationStart()

Use singleton pattern - Access same server by name throughout app

Secure endpoints - Validate authentication before handling requests

Set CORS carefully - Explicitly whitelist allowed origins

Document tools - Provide clear descriptions for AI clients

Handle errors - Wrap tool handlers in try/catch for robustness

Don't create per request - Servers are singletons, get by name

Don't allow all CORS - Never use * in production

Don't skip authentication - Always verify requests in public endpoints

Don't register duplicate names - Tool/resource/prompt names must be unique

Last updated