Loading page...Загружаем страницу...正在加载页面...
AtlasRepo

Building Your First MCP Server: Exposing Internal Tools to AI Agents Safely

Description and use

Building a TypeScript MCP server that safely exposes internal tools to AI agents with strict input schemas, read‑only defaults, pending refund requests, and audit logging.

Problem

Expose internal order lookup and refund request tools to AI agents while ensuring safety and auditability.

Outcome

A functional MCP server that safely exposes internal tools, with audit logging and strict schemas, enabling AI agents to interact without risking unintended actions.

Workflow

  • 1. Initialize project with npm and TypeScript.
  • 2. Install @modelcontextprotocol/sdk, zod, and dev dependencies.
  • 3. Add "type":"module" to package.json and create src/index.ts.
  • 4. Import McpServer, StdioServerTransport, zod, audit, and backend functions.
  • 5. Create McpServer instance with name and version.
  • 6. Register "lookup_order" tool with title, description, inputSchema (orderId regex), outputSchema (orderId, status, totalCents, itemCount), and annotations (readOnlyHint).
  • 7. Implement async handler: fetch order via getOrder, audit call, return error or structured result.
  • 8. Register transport (StdioServerTransport) and connect server.
  • 9. Log server running message.
  • 10. (Optional) Extend with refund request tool following similar pattern.

Evidence

Instead of writing a custom integration for every model and every client, you write one MCP server, and any MCP-capable client can discover and call its tools.

Source

apply safety patterns from the start: strict input schemas, read-only by default, write actions that only create pending requests, and an audit log of every call.

Source

server.registerTool("lookup_order", { title: "Look up an order", description: "Returns status, items and totals for one order. Read-only.", ...});

Source

  • Record date: 2026-10-18T03:41:18.800Z