Build your first MCP server tool
Register a local MCP tool with a small input schema, then use a stdio client to list it and verify one result.
You have a local function that converts minutes to seconds, and you want an agent to call it with a number. Give the function a name, an input schema, and a response the client can read. An MCP server can expose all three over the same pair of process pipes.
Register the tool
In an empty folder, create a package and install the SDK and schema library.
npm init -y
npm install @modelcontextprotocol/sdk@1.27.1 zod@3.25.76You now have the SDK's server and client entry points in the package.
Save this as server.mjs. The SDK’s tool registration exposes the tool name, description and input schema to the client. The return value wraps the string representation of the result in a text content item.
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: 'time-tools', version: '1.0.0' })
server.registerTool('minutes_to_seconds', {
description: 'Convert a whole number of minutes to seconds',
inputSchema: { minutes: z.number().int().min(0).max(1440) },
}, ({ minutes }) => ({
content: [{ type: 'text', text: String(minutes * 60) }],
}))
await server.connect(new StdioServerTransport())Running node server.mjs on its own waits for a client to send a message on stdin.
List and call it from a client
The client launches the server as a child process and initializes a connection. It can then list the tools and send a call with arguments. The stdio transport uses newline-delimited JSON-RPC messages.
diagram
A tool call over two pipes
Save this as client.mjs beside the server. It resolves the server path relative to the client file, so the command can run with a different working directory.
import assert from 'node:assert/strict'
import { Client } from '@modelcontextprotocol/sdk/client/index.js'
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js'
import { fileURLToPath } from 'node:url'
const client = new Client({ name: 'time-tools-test', version: '1.0.0' })
const transport = new StdioClientTransport({
command: process.execPath,
args: [fileURLToPath(new URL('./server.mjs', import.meta.url))],
})
try {
await client.connect(transport)
const { tools } = await client.listTools()
console.log(tools.map(tool => tool.name).join(', '))
const result = await client.callTool({
name: 'minutes_to_seconds',
arguments: { minutes: 3 },
})
assert.deepEqual(result.content, [{ type: 'text', text: '180' }])
console.log(result.content[0].text)
} finally {
await client.close()
}Run node client.mjs to see minutes_to_seconds and then 180. The assertion checks the response before the client closes the connection.
A compatible host can use the same launch command, node followed by the absolute path to server.mjs. Its tool picker can display the registration once the process connects. Keep stdout for protocol traffic. If you need to see whether the server started, write the log to stderr with console.error.
Change the call to minutes: 3.5 and run the client again. On SDK 1.27.1, callTool rejects with JSON-RPC error -32602 before the handler runs. The client closes the connection in finally; execution skips the result assertion.