For the complete documentation index, see llms.txt. This page is also available as Markdown.

Class-Based Servers

For larger applications, encapsulate your entire MCP server in a dedicated class that extends MCPServer. This provides better organization, reusability, and testability.

Overview

Instead of creating inline servers with MCPServer( "name" ), extend the MCPServer class and register it with aiService().putServer().

Basic Class Structure

// mcp/MyAppServer.bx
class extends="MCPServer" {

    function init() {
        // Initialize the parent MCPServer
        super.init(
            name: "my-app",
            description: "My Application MCP Server",
            version: "1.0.0"
        )

        // Register capabilities in constructor
        registerTools()
        registerResources()
        registerPrompts()
        configureSecurity()

        return this
    }

    function registerTools() {
        this.registerTool(
            aiTool( "search", "Search documents", query => searchService.search( query ) )
        )
        this.registerTool(
            aiTool( "calculate", "Perform calculations", expr => evaluate( expr ) )
        )
    }

    function registerResources() {
        this.registerResource(
            uri: "docs://api",
            name: "API Documentation",
            description: "API reference",
            mimeType: "text/markdown",
            handler: () => fileRead( expandPath( "/docs/api.md" ) )
        )
    }

    function registerPrompts() {
        this.registerPrompt(
            name: "summarize",
            description: "Summarize a document",
            args: [
                { name: "text", description: "Text to summarize", required: true }
            ],
            handler: ( args ) => [
                { role: "system", content: "You are a summarization assistant." },
                { role: "user", content: "Summarize: #args.text#" }
            ]
        )
    }

    function configureSecurity() {
        this.withBasicAuth( getEnv( "MCP_USER" ), getEnv( "MCP_PASS" ) )
            .withCors( "https://myapp.com" )
    }

}

Register at Application Start

Benefits vs Inline

Inline MCPServer()

Class-Based extends="MCPServer"

Quick setup for simple servers

Better organization for complex servers

All config in one place

Separation of concerns

Harder to test in isolation

Easily unit-testable

Good for scripts & prototypes

Good for production applications

Supports inheritance & composition

Multiple Server Classes

Create different server classes for different purposes:

Using Annotation Discovery in Classes

Combine class structure with automatic discovery:

Inheritance & Composition

Base Server Class

Specialized Server

Configuration via Constructor Parameters

Using Configurable Server

Unit Testing Class-Based Servers

Next Steps

Last updated