Skip to main content

Quick Start Guide

Follow this guide to build a simple Calculator Tool MCP server in just a few minutes.

What you'll build: A functional MCP server with a calculator tool that can perform basic arithmetic operations.

Time required: ~10 minutes

Prerequisites

Before you begin, make sure you have:

  • Node.js 16.0.0 or later
  • npm or yarn
  • TRMX CLI installed (see Installation Guide)
  • Basic JavaScript knowledge

Step 1: Create a New MCP Project

Start by creating a new MCP project using the TRMX CLI:

# Create a new project with the JavaScript template
trmx init my-calculator-mcp --template javascript

# Navigate to the project directory
cd my-calculator-mcp

# Install dependencies
npm install

Step 2: Create the Calculator Tool

Create a new file src/tools/calculator.js with the following code:

// src/tools/calculator.js
export const calculatorTool = {
name: 'calculator',
description: 'Performs basic arithmetic operations',
parameters: {
type: 'object',
properties: {
operation: {
type: 'string',
enum: ['add', 'subtract', 'multiply', 'divide'],
description: 'The operation to perform',
},
a: {
type: 'number',
description: 'First operand',
},
b: {
type: 'number',
description: 'Second operand',
},
},
required: ['operation', 'a', 'b'],
},
execute: async ({ operation, a, b }) => {
switch (operation) {
case 'add': return { result: a + b };
case 'subtract': return { result: a - b };
case 'multiply': return { result: a * b };
case 'divide':
if (b === 0) throw new Error('Division by zero');
return { result: a / b };
default:
throw new Error(`Unknown operation: ${operation}`);
}
},
};

Step 3: Register the Tool

Now, register the tool in src/tools/index.js:

// src/tools/index.js
import { calculatorTool } from './calculator';

export const tools = [
calculatorTool,
// Other tools...
];

Step 4: Start Your MCP Server

Start your MCP server in development mode:

# Start the server
npm run dev

You should see a message that your MCP server is running on http://localhost:3000.

💡 The development server automatically reloads when you make changes to your code.

Step 5: Test the Calculator Tool

Let's test the calculator tool using the TRMX CLI:

# In a new terminal window
trmx test --server http://localhost:3000 --tool calculator --args '{"operation":"add","a":5,"b":3}'

You should see a successful response like this:

{
"result": 8
}

Try different operations:

Multiply

trmx test --server http://localhost:3000 --tool calculator --args '{"operation":"multiply","a":4,"b":7}'

Output:

{
"result": 28
}

Subtract

trmx test --server http://localhost:3000 --tool calculator --args '{"operation":"subtract","a":10,"b":6}'

Output:

{
"result": 4
}

Divide

trmx test --server http://localhost:3000 --tool calculator --args '{"operation":"divide","a":20,"b":5}'

Output:

{
"result": 4
}

Step 6: Create a Simple Client Application

Let's create a simple client application to use our calculator. Create a new file client.js in the project root:

// client.js
const { MCPClient } = require('@trmx/client');

async function main() {
// Create an MCP client
const client = new MCPClient({
serverUrl: 'http://localhost:3000',
});

// List available tools
const tools = await client.request({ method: 'tools/list' });
console.log('Available tools:', tools.result);

// Call the calculator tool
const result = await client.request({
method: 'tools/call',
params: {
name: 'calculator',
arguments: {
operation: 'multiply',
a: 6,
b: 7,
},
},
});

console.log('Calculation result:', result.result);
}

main().catch(console.error);

Run the client:

node client.js

You should see output like this:

Available tools: [
{
name: 'calculator',
description: 'Performs basic arithmetic operations',
parameters: { /* parameter definition */ }
}
]
Calculation result: { result: 42 }

Step 7: Connect to an AI Model

Now, let's create an application that connects our MCP server to an AI model. First, install the required dependencies:

npm install openai dotenv

Create a .env file in your project root:

OPENAI_API_KEY=your_openai_api_key_here

Create a file ai-client.js:

// ai-client.js
const { MCPClient } = require('@trmx/client');
const { OpenAI } = require('openai');
require('dotenv').config();

async function main() {
// Create an MCP client
const mcpClient = new MCPClient({
serverUrl: 'http://localhost:3000',
});

// Create an OpenAI client
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});

// Get tools from MCP server
const toolsResponse = await mcpClient.request({ method: 'tools/list' });
const tools = toolsResponse.result.map(tool => ({
type: 'function',
function: {
name: tool.name,
description: tool.description,
parameters: tool.parameters,
},
}));

// Chat with the AI model
const messages = [
{ role: 'system', content: 'You are a helpful assistant with access to a calculator tool.' },
{ role: 'user', content: 'What is 123 multiplied by 456?' },
];

const response = await openai.chat.completions.create({
model: 'gpt-4-turbo',
messages,
tools,
tool_choice: 'auto',
});

const responseMessage = response.choices[0].message;
console.log('AI response:', responseMessage);

// Handle tool calls
if (responseMessage.tool_calls) {
const toolCall = responseMessage.tool_calls[0];
console.log('Tool call:', toolCall);

// Parse the arguments
const args = JSON.parse(toolCall.function.arguments);

// Call the MCP server
const result = await mcpClient.request({
method: 'tools/call',
params: {
name: toolCall.function.name,
arguments: args,
},
});

console.log('Tool result:', result);

// Send the result back to the AI
messages.push(responseMessage);
messages.push({
role: 'tool',
tool_call_id: toolCall.id,
content: JSON.stringify(result),
});

// Get the final response
const finalResponse = await openai.chat.completions.create({
model: 'gpt-4-turbo',
messages,
});

console.log('Final AI response:', finalResponse.choices[0].message.content);
}
}

main().catch(console.error);

Run the AI client:

node ai-client.js

If everything is set up correctly, you should see the AI model call your calculator tool to compute 123 × 456 and return the answer.

What's Next?

Congratulations! You've successfully built your first MCP server with a calculator tool and connected it to an AI model. Here are some next steps to explore:

Create More Tools

Add more tools to your MCP server like a weather API, data retrieval, etc. Learn More

Add Resources

Learn how to add resources to your MCP server to provide data access Explore Resources

Deploy Your MCP

Deploy your MCP server to production Deployment Guide