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 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/function
This 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-server
Inside your project, you’ll find the following files:
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.
src/gram.ts
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 buildnpm run push
This 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.
Step 4: Create an MCP server
You can now add your tools to an existing MCP server or create a new one.
To create a new MCP server, go to the Toolsets page and click Create A Toolset (or + Add Toolset). Name it whatever you want. A toolset is a group of tools that can be exposed as an MCP server.
For more information on toolsets, see the Toolsets documentation.
Now, click Add Tools to add your tools to the toolset. You’ll see the tools you defined in your src/gram.ts file listed here.
Finally, head to the MCP tab on the toolset page and click Enable. Check out the Install Page linked therein for help installing the MCP server in your favorite client. For more information on MCP servers, see the MCP Servers documentation.
What’s next?
The following resources will help you get the most out of your Gram MCP server: