Skip to content

Organizing your Code

You can define and export your tools, prompts or skills within the mcp.ts but we recommend splitting them for readability and maintainability.

The bare minimum structure as mentioned would be as follows:

  • …
  • Directorysrc
    • mcp.ts all tools, prompts, or skills can be defined and exported
  • package.json
  • README.md

The mcp.ts could looks something like this:

import { z } from "zod";
import { defineTool } from "@mcpexpress/core";
export const getWeather = defineTool({
name: "get_weather",
description: "Current weather for a city",
input: z.object({ city: z.string() }),
execute: async ({ city }, { signal }) => `It is sunny in ${city}`,
});

While this might be the simplest solution for small projects but as the complexity increases, this becomes unmanageable. Therefore we recommend the following structure:

  • …
  • Directorysrc
    • Directorytools contains tool code
      • …
    • Directoryprompts contains prompts
      • …
    • Directoryskills contains skills
      • …
    • mcp.ts all tools, prompts, or skills are exported here
  • package.json
  • README.md

The same weather tool defined above could be better organized if split in two different files.

src/tools/weather.ts
import { z } from "zod";
import { defineTool } from "@mcpexpress/core";
export const getWeather = defineTool({
name: "get_weather",
description: "Current weather for a city",
input: z.object({ city: z.string() }),
execute: async ({ city }, { signal }) => `It is sunny in ${city}`,
});

You can use the mcp.ts to export the tool and make it available within MCP Express.

src/mcp.ts
export { getWeather } from "./tools/weather.js";

This enables you to organize your code cleaner and only the export definitions reside within the mcp.ts.