Swiftorial Logo
Home
Swift Lessons
AI Tools
Learn More
Career
Resources

3. How do you build and expose a custom tool using MCP?

MCP makes it easy to expose new tools to LLMs through a simple, structured interface. The core idea is that any API or microservice that accepts a POST request to /invoke and returns a JSON output field can become an MCP server.

🔨 Step-by-Step Guide:

  1. Choose a task for your tool (e.g., calculate tax, format markdown, fetch calendar events).
  2. Create a web server that handles POST requests on /invoke.
  3. Define a clear input and output shape (JSON format).
  4. Add input validation and safety logic.
  5. Register your server with the MCP client or tooling layer (e.g., Claude Desktop).

📦 Example: Markdown Formatter Tool

// markdown-server.ts
import express from 'express';
import { marked } from 'marked';

const app = express();
app.use(express.json());

app.post('/invoke', (req, res) => {
  const { markdown } = req.body.input;

  if (typeof markdown !== 'string') {
    return res.status(400).json({ error: 'Input must contain markdown string' });
  }

  const html = marked(markdown);
  res.json({ output: { html } });
});

app.listen(5000, () => console.log('MCP Markdown Server running'));

🎯 How an LLM Would Call It:

{
  tool: "markdown-server",
  input: {
    markdown: "# Hello, **ThinkWing**!"
  }
}

📘 Implementation Tips:

  • Schema Discipline: Define and document expected input/output types.
  • Error Handling: Return helpful error messages in a predictable structure.
  • Modularity: One tool = one job. Avoid bundling too many unrelated actions.
  • Local Testing: Use tools like Postman or curl to test your /invoke endpoint before connecting it to an LLM.

🧰 Great Tool Ideas to Start With:

  • Date parser or time zone converter
  • Crypto wallet checker or currency formatter
  • File summarizer (via PDF or text extraction)
  • Email normalizer or validation helper