An MCP server can contribute tools, resources, and prompts to M31A. This tutorial builds the smallest useful server: one tool that echoes a message. The client lifecycle lives in packages/core/src/tools/mcp-client-manager.ts.
Prerequisites
- Node.js 20 or newer and npm.
- M31A installed and authenticated.
- A throwaway directory for the server code.
1. Create the project
mkdir -p /tmp/m31a-mcp-demo && cd /tmp/m31a-mcp-demo
npm init -y
npm install @modelcontextprotocol/sdk zodSet "type": "module" in package.json so the server can use top-level await, as the bundled example in bundle/examples/mcp-server/example.js does.
2. Write the server
Create server.js:
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { z } from 'zod';
const server = new McpServer({
name: 'echo-server',
version: '1.0.0',
});
server.registerTool(
'echo',
{
description: 'Echo a short message back to the caller.',
inputSchema: {
message: z.string().describe('Text to echo'),
},
},
async ({ message }) => ({
content: [{ type: 'text', text: `Echo: ${message}` }],
}),
);
const transport = new StdioServerTransport();
await server.connect(transport);Important details:
inputSchemauses theshapestyle shown in the bundled example:z.object({...}).shape, which is equivalent to the plain object used above.- Write protocol traffic only to stdout; use stderr for logs. A stray
console.logon stdout will corrupt the JSON-RPC stream. - The process stays alive until M31A closes the transport.
3. Smoke-test the server directly
node server.jsThe process should wait silently. Stop it with Ctrl+C. A silent stdio server is expected; there is no interactive prompt.
4. Register it with M31A
m31a mcp add echo-demo node /tmp/m31a-mcp-demo/server.js --scope user
m31a mcp listmcp add accepts a command plus arguments or a URL. Useful options include --transport stdio|sse|http, --env KEY=value, --header, --timeout, --description, --trust, --include-tools, and --exclude-tools. --scope defaults to project; use user to make the server available across repositories.
You can also edit settings directly:
{
"mcpServers": {
"echo-demo": {
"command": "node",
"args": ["/tmp/m31a-mcp-demo/server.js"],
"cwd": "/tmp/m31a-mcp-demo",
"timeout": 30000
}
},
"mcp": {"allowed": ["echo-demo"], "excluded": []}
}5. Verify the schema
Start the TUI and check that the tool is visible:
/mcp list
/mcp schema/mcp schema prints each server's tools with descriptions and parameter schemas. You should see an echo tool with a required message string field. Then try it:
Use the echo-demo echo tool with the message "hello from M31A".MCP tools are named with a server prefix, so the tool appears internally as mcp_echo-demo_echo. The prefix is how policy rules target a specific server.
6. Handle confirmation
By default, an MCP tool call can require confirmation. You have three options:
- Approve interactively, and optionally choose always-allow.
- Register the server with
--trust(or setmcpServers.echo-demo.trust). - Allow the server persistently with
mcp.allowed.
Do not trust a server you did not write or audit: it can contribute arbitrary tool definitions and execute code with your privileges. See MCP security controls.
7. Headless verification
m31a -p "Call the echo tool with message CI-check and report the exact text" \
--allowed-mcp-server-names echo-demo -o json--allowed-mcp-server-names restricts which servers load for one invocation. In headless mode, a tool call that would need confirmation is denied rather than prompting, so keep the server allowlisted or trusted for scripted use.
8. Troubleshooting
| Symptom | Resolution |
|---|---|
| Server not listed | Run m31a mcp list; check mcp.excluded and enablement. |
| Connection fails | Confirm node is on PATH and the path in args is correct. |
| Tool name unclear | Use /mcp schema or /tools desc. |
| Protocol parse errors | Remove stdout logging from the server. |
| Headless denial | Trust the server or add it to mcp.allowed. |
Next steps
- Read MCP for resources, prompts, OAuth, and admin controls.
- Read Extension model to see how servers are shipped inside an extension.
- Continue with Custom extension.