Create an MCP Server using the TypeScript Framework (Gram Functions)
When you need to build tools with custom business logic, data transformations, or multi-step workflows, Gram Functions provides a TypeScript framework for creating serverless functions that can be exposed as MCP server tools. Unlike OpenAPI-sourced tools which simply wrap existing API endpoints, Gram Functions allow you to write custom code that executes in isolated environments.
Gram Functions-sourced MCP tools are ideal for the following use cases:
- Custom business logic: Implement complex calculations, data processing, or decision-making logic that goes beyond simple API calls. For example, aggregating data from multiple sources or applying custom validation rules.
- Data transformations: Transform, filter, or enrich data before returning it to LLM clients. For example, parsing and summarizing large datasets or converting between data formats.
- Multi-step workflows: Orchestrate multiple API calls or operations in a single tool. For example, creating a tool that searches for data, processes it, and then stores the results.
This guide walks through the steps of building an MCP server with Gram Functions. This includes:
- Creating a new Gram Functions project,
- Writing custom tools with TypeScript,
- Deploying your functions to Gram, and
- Creating an MCP server from your tools.
Before you start
This guide assumes that you have already done the following:
- Created a Gram account ,
- Created a Gram project (accomplished during onboarding), and
- Installed Node.js version >=22.18.0.
Quick Example
Here’s a simple example of a Gram Function that greets a user:
import { Gram } from "@gram-ai/functions";
import * as z from "zod/mini";
const gram = new Gram().tool({
name: "greet",
description: "Greet someone special",
inputSchema: { name: z.string() },
async execute(ctx, input) {
return ctx.text(`Hello, ${input.name}!`);
},
});
export default gram;Step 1: Create a project
Gram offers a bootstrapper through npm/pnpm create to create a new project.
Note
Gram Functions currently requires a node version >=22.18.0
npm create @gram-ai/functionThis will prompt you to pick a framework.
◆ Pick a framework
│ ● Gram (Default. A simple framework to get started quickly)
│ ○ MCP (Gram also supports the official MCP SDK for more advanced use cases)This will create a new project in the current directory.
cd my-mcp-serverInside your project, you’ll find the following files:
└── src
├── gram.ts <- Edit me!
└── server.ts
└── package.json
└── README.md
...Step 2: Write your tools
Open src/gram.ts and write your tools. You can add as many tools as you want, or leave it as-is to use the default tool. For more information on writing tools, see the Gram Functions documentation.
import { Gram } from "@gram-ai/functions";
import * as z from "zod/mini";
const gram = new Gram().tool({
name: "greet",
description: "Greet someone special",
inputSchema: { name: z.string() },
async execute(ctx, input) {
return ctx.json({ message: `Hello, ${input.name}!` });
},
});
export default gram;Note
You are not necessarily building an MCP server here. Write whatever tools you want—you will later be able to slice and dice them into MCP servers or combine them with other types of tools into existing MCP servers.
Step 3: Build and deploy
To build and deploy your MCP server, run the following commands:
npm run build
npm run pushThis will build your functions and deploy them to Gram. In the Gram Dashboard , you will see the function source you just uploaded and the tools it created.