Skip to content

MCP Agent Server

The MCP Agent Server is an MCP server where each tool is backed by an AI agent loop. When an AI client calls a tool, the server starts an LLM agent that iteratively calls FlowMCP schema tools to solve the problem and returns a structured answer.

AI Client --> MCP Protocol --> AgentToolsServer
|
ToolRegistry
|
AgentLoop
/ \
LLM (OpenRouter) ToolClient
|
FlowMCP Schemas
|
External APIs

The AI client sends a request to the MCP server. The server starts an agent loop that uses an LLM to decide which FlowMCP tools to call, calls them, and iterates until the problem is solved. The final answer is returned to the AI client.

Terminal window
npm install mcp-agent-server

Or clone and run locally:

Terminal window
git clone https://github.com/FlowMCP/mcp-agent-server.git
cd mcp-agent-server
npm install

The server mounts as Express middleware:

import express from 'express'
import { AgentToolsServer } from 'mcp-agent-server'
const app = express()
app.use( express.json() )
const { mcp } = await AgentToolsServer.create( {
name: 'My Agent Server',
version: '1.0.0',
routePath: '/mcp',
llm: {
baseURL: 'https://openrouter.ai/api',
apiKey: process.env.OPENROUTER_API_KEY
},
tools: [
{
name: 'defi-research',
description: 'Research DeFi protocols and TVL data',
inputSchema: {
type: 'object',
properties: {
query: { type: 'string', description: 'Research query' }
},
required: [ 'query' ]
},
agent: {
systemPrompt: 'You are a DeFi research agent. Use the available tools to answer questions about DeFi protocols, TVL, and market data.',
model: 'anthropic/claude-sonnet-4-5-20250929',
maxRounds: 10,
maxTokens: 4096
},
toolSources: [
{
type: 'flowmcp',
schemas: [ defilamaSchema, coingeckoSchema ],
serverParams: { DEFILAMA_KEY: process.env.DEFILAMA_KEY }
}
]
}
]
} )
app.use( mcp.middleware() )
app.listen( 4100 )

Creates a new MCP server instance from configuration.

const { mcp } = await AgentToolsServer.create( {
name: 'My Server',
version: '1.0.0',
routePath: '/mcp',
llm: { baseURL, apiKey },
tools: [ /* tool configs */ ],
tasks: { store: customTaskStore } // optional
} )
KeyTypeDescriptionRequired
namestringServer name for MCP handshakeYes
versionstringServer versionYes
routePathstringExpress route path (default '/mcp')No
llmobjectLLM config { baseURL, apiKey }Yes
toolsarrayArray of tool configurationsYes
tasksobjectTask store config (default InMemoryTaskStore)No

Returns an Express middleware that handles MCP protocol requests (POST/GET/DELETE) on the configured route path.

app.use( mcp.middleware() )

Returns all registered tools in MCP ListTools format.

const { tools } = mcp.listToolDefinitions()
// tools: [ { name, description, inputSchema, execution? } ]

Each tool in the tools array defines an agent-powered MCP tool:

KeyTypeDescriptionRequired
namestringTool name used in MCP protocolYes
descriptionstringWhat this tool doesYes
inputSchemaobjectJSON Schema for tool inputYes
agentobjectAgent configurationYes
toolSourcesarrayWhere the agent gets its tools fromYes
executionobject{ taskSupport: 'optional' | 'required' }No
KeyTypeDescriptionRequired
systemPromptstringSystem prompt for the LLMYes
modelstringLLM model ID (e.g., 'anthropic/claude-sonnet-4-5-20250929')Yes
maxRoundsnumberMaximum agent iterations (default 10)No
maxTokensnumberMax completion tokens (default 4096)No
answerSchemaobjectCustom JSON Schema for the submit_answer toolNo

Each entry in toolSources defines where the agent gets its tools:

KeyTypeDescriptionRequired
typestringSource type (currently 'flowmcp')Yes
schemasarrayFlowMCP schema objectsYes
serverParamsobjectAPI keys and environment variablesNo

Import FlowMCP schemas and pass them as tool sources:

import { main as defilamaMain } from './schemas/defillama.mjs'
import { main as coingeckoMain } from './schemas/coingecko.mjs'
const tools = [
{
name: 'defi-analyst',
description: 'Analyze DeFi protocols',
inputSchema: { /* ... */ },
agent: {
systemPrompt: 'You are a DeFi analyst.',
model: 'anthropic/claude-sonnet-4-5-20250929',
maxRounds: 10,
maxTokens: 4096
},
toolSources: [
{
type: 'flowmcp',
schemas: [ defilamaMain, coingeckoMain ],
serverParams: {
COINGECKO_API_KEY: process.env.COINGECKO_API_KEY
}
}
]
}
]

Add payment gating with no code coupling — pure Express middleware ordering:

import express from 'express'
import { X402Middleware } from 'x402-mcp-middleware/v2'
import { AgentToolsServer } from 'mcp-agent-server'
const app = express()
app.use( express.json() )
// 1. Payment gate (optional)
const x402 = await X402Middleware.create( { /* config */ } )
app.use( x402.mcp() )
// 2. Agent MCP Server
const { mcp } = await AgentToolsServer.create( { /* config */ } )
app.use( mcp.middleware() )
app.listen( 4100 )
  • StreamableHTTP transport with session-based connections
  • LLM Agent Loop with iterative tool calling via Anthropic SDK
  • FlowMCP schemas as in-process tool sources (no external server)
  • Configurable answer schema per tool for structured outputs
  • MCP Tasks API for async tool execution
  • x402 composition for payment gating via middleware ordering
  • Multiple tool sources per tool via CompositeToolClient