# 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](https://app.getgram.ai),
- Created a Gram project (accomplished during onboarding), and
- Installed Node.js version >=22.18.0.

<Callout title="Quick Example" type="info">
  Here's a simple example of a Gram Function that greets a user:

```typescript

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}!`);
  },
});

```
</Callout>

## Step 1: Create a project

Gram offers a bootstrapper through `npm/pnpm create` to create a new project.

<Callout title="Note" type="info">
  Gram Functions currently requires a node version >=22.18.0
</Callout>

```bash
npm create @gram-ai/function
```

This will prompt you to pick a framework.
```bash
◆  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.
```bash
cd my-mcp-server
```

Inside your project, you'll find the following files:
```bash
└── 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](/docs/mcp/build/gram-functions) documentation.

```typescript filename="src/gram.ts"

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}!` });
  },
});

```

<Callout title="Note" type="info">
  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.
</Callout>

## Step 3: Build and deploy

To build and deploy your MCP server, run the following commands:

```bash
npm run build
npm run push
```

This will build your functions and deploy them to Gram. In the [Gram Dashboard](https://app.getgram.ai), you will see the function source you just uploaded and the tools it created.

![Gram Dashboard, showing the function source you just uploaded](/assets/docs/gram/img/functions/onboarding_functions-pushed.png)

## 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](/docs/mcp/build/toolsets/create-default-toolset) documentation.

![Gram Dashboard, showing the create toolset dialog](/assets/docs/gram/img/functions/onboarding_create-toolset.png)

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.

![Gram Dashboard, showing the add tools dialog](/assets/docs/gram/img/functions/onboarding_add-tools.png)

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](/docs/mcp/build/hosting/deploy-mcp-server) documentation.

## What's next?

The following resources will help you get the most out of your Gram MCP server:

- [Learn more about the Gram Functions framework.](/docs/mcp/build/gram-functions)
- [Learn about best practices for curating toolsets.](/docs/mcp/build/toolsets/advanced-tool-curation)
- [Tag tools so clients can filter by tag.](/docs/mcp/build/gram-functions/tool-tags)
- [Add OpenAPI-sourced tools to your MCP server.](/docs/mcp/getting-started/openapi)
