Speakeasy Logo
Skip to Content

Using the MCP SDK

Overview

The Gram MCP SDK wrapper provides lower-level access to MCP protocol features. This allows you to deploy an MCP server built using the modelcontextprotocol  SDK to Gram functions.

Choosing MCP SDK

Basic usage

To deploy an existing MCP server or new MCP server to Gram Functions using the modelcontextprotocol SDK you will first need a file that exports an instance of McpServer.

import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { z } from "zod"; export const server = new McpServer({ name: "demo-server", version: "1.0.0", }); server.registerTool( "add", { title: "Addition Tool", description: "Add two numbers", inputSchema: { a: z.number(), b: z.number() }, }, async ({ a, b }) => { const output = { result: a + b }; return { content: [{ type: "text", text: JSON.stringify(output) }], }; }, );

A simple gram.ts file then wraps that server so it can be deployed to Gram Functions.

gram.ts
import { withGram } from "@gram-ai/functions/mcp"; import { server } from "./mcp.ts"; export default withGram(server);

Resources

MCP resources can also be deployed via Gram Functions when using the MCP SDK wrapper.

import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { z } from "zod"; export const server = new McpServer({ name: "demo-server", version: "1.0.0", }); server.registerResource( "a-cool-photo", "resources://a-cool-photo", { mimeType: "image/jpg", description: "This photo is really something", title: "A Cool Photo", }, async (uri) => { let res = await fetch("https://picsum.photos/200/300.jpg"); return { contents: [ { uri: uri.href, blob: Buffer.from(await res.arrayBuffer()).toString("base64"), }, ], }; }, );

After running push simply select that resource to add it to a toolset. Adding a Resource

Environment Variables

You can specify credentials or envrionment variable values that will need to be made available in your tool runner envrionment. These can be provided either by end user set MCP headers or with stored Gram environments.

gram.ts
import { withGram } from "@gram-ai/functions/mcp"; import { server } from "./mcp.ts"; export default withGram(server, { variables: { API_KEY: { description: "API key for authentication" }, }, });

Next steps

Last updated on