Advanced โฑ 8 min read ๐Ÿ“ examples/25_mcp_server_http.mjs

Lesson 37: Your SDK is the MCP server

your SDK as an MCP server over Streamable HTTP, capabilities, artifacts, loadable components

1. Why This Feature Exists

This process does not consume someone else's MCP server; it IS one. A node:http server serves the Streamable HTTP transport from createMcpHttpHandler, and a real model on the other side of an McpClient calls the tools this same process advertises โ€” the same model wearing two hats, server-side and client-side.

2. What the Example Demonstrates

A โ€” serve
createMcpHttpHandler mounts word_stats (with an outputSchema) on a loopback node:http server.
B โ€” connect
an McpClient attaches over Streamable HTTP and loadMcpTools maps the advertised tool into an SDK tool; a direct call returns the server's own numbers.
C โ€” the model calls
a live session uses that remote tool and answers with the forced marker plus the real word count.
D โ€” the extension surface
a LoadableComponent (loaded, saved and injected as a per-step block), an ExternalToolCapability doing real file work, and an AcpCapability over a duck-typed client โ€” all registered on one session with withCapability.
E โ€” bounded output
capToolOutput on text, capToolOutputValue on JSON, with the truncation suffix the model actually sees.
F โ€” artifacts
createSaveArtifactTool writes versioned files and keeps a manifest, so a large result becomes a path.
G โ€” host context
buildEnvironmentBlock + discoverInstructions render the workspace block and the AGENTS.md a host injects. What this shows: - the Streamable HTTP server and the client SDK meeting over loopback - capabilities as the extension seam (component + remote families) - output caps and artifacts as the boundary for large results Run: node examples/25_mcp_server_http.mjs Needs: PROVIDER + provider credentials in secret.env. @version 0.27.0
imported from 'merci-sdk'
AcpCapability, ExternalToolCapability, LoadableComponent, McpClient, buildEnvironmentBlock, capToolOutput, capToolOutputValue, createMcpHttpHandler, createSaveArtifactTool, defaultDisplay, discoverInstructions, initializeClient, loadCredentials, loadMcpTools, log

3. How the Core Call Works

The entry point, condensed. The full runnable file is linked below.

import { createHash } from 'node:crypto';
import { createServer } from 'node:http';
import { mkdir, readFile, rm, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import {
    LoadableComponent,
    buildEnvironmentBlock, createSaveArtifactTool,
    defaultDisplay, discoverInstructions, initializeClient, loadCredentials, log,
} from 'merci-sdk';
import {
    AcpCapability, ExternalToolCapability, McpClient, capToolOutput, capToolOutputValue,
    createMcpHttpHandler, loadMcpTools, TOOL_OUTPUT_TRUNCATION_SUFFIX,
} from 'merci-sdk/tools';
import { SCRATCH_DIR } from './_shared/real-tools.mjs';
import { reportUsage, resolveExample, runMain, trackUsage, warnEmptyAnswer, wireClient } from './_shared/example-utils.mjs';

const RUN_DIR = join(SCRATCH_DIR, '25-mcp-http');
const WORKSPACE = join(RUN_DIR, 'workspace');
const ARTIFACTS = join(RUN_DIR, 'artifacts');
const NOTES_FILE = join(WORKSPACE, 'release-notes.md');
const SAMPLE_TEXT = 'the quick brown fox jumps over the lazy dog';
const SERVER_MARKER = 'LIVE-MCP-HTTP';
const EXTENSION_MARKER = 'EXTENSION-OK';
const FILE_TOKEN = 'ORBIT-7';
const RETRY_BUDGET = '3';
const ARTIFACT_TEXT = '# Staging report\n\nThe release went out at 09:12.';

/**
 * One word-statistics tool served by this process. `outputSchema` makes the MCP
 * server return `structuredContent`, not just a text blob.
 */
const wordStatsTool = {
    name: 'word_stats',
    description: 'Computes text statistics: characters, words, lines and the most frequent words.',
    parameters: {
        type: 'object',
        properties: {
            text: { type: 'string', description: 'The text to analyze.' },
            top: { type: 'integer', minimum: 1, maximum: 10, description: 'How many frequent words to return (default 3).' },
        },
        required: ['text'],
    },
    outputSchema: {
        type: 'object',

Read the full example: examples/25_mcp_server_http.mjs โ†’

4. Behavior the Code Fixes

This process does not consume someone else's MCP server; it IS one. A node:http server serves the Streamable HTTP transport from createMcpHttpHandler, and a real model on the other side of an McpClient calls the tools this same process advertises โ€” the same model wearing two hats, server-side and client-side.

5. Run the Example

Execute the runnable example from the repository root:

node examples/25_mcp_server_http.mjs
What it does: your SDK as an MCP server over Streamable HTTP, capabilities, artifacts, loadable components