@gram-ai/create-function 0.6.2 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/gram-template-gram/CONTRIBUTING.md +41 -32
- package/gram-template-gram/README.md +7 -1
- package/gram-template-gram/gitignore +2 -1
- package/gram-template-gram/package.json +2 -1
- package/gram-template-mcp/README.md +49 -0
- package/gram-template-mcp/gitignore +2 -1
- package/gram-template-mcp/package.json +2 -1
- package/gram-template-mcp/src/gram.ts +8 -2
- package/package.json +2 -2
|
@@ -23,7 +23,8 @@
|
|
|
23
23
|
|
|
24
24
|
### The Gram Instance
|
|
25
25
|
|
|
26
|
-
The `Gram` class is the main entry point for defining tools. You create an
|
|
26
|
+
The `Gram` class is the main entry point for defining tools. You create an
|
|
27
|
+
instance and chain `.tool()` calls to register multiple tools:
|
|
27
28
|
|
|
28
29
|
```typescript
|
|
29
30
|
import { Gram } from "@gram-ai/functions";
|
|
@@ -57,7 +58,6 @@ Each tool requires:
|
|
|
57
58
|
- **name**: A unique identifier for the tool
|
|
58
59
|
- **description** (optional): Human-readable description of what the tool does
|
|
59
60
|
- **inputSchema**: A Zod schema object defining the expected input parameters
|
|
60
|
-
- **variables** (optional): Environment variables the tool needs
|
|
61
61
|
- **execute**: An async function that implements the tool logic
|
|
62
62
|
|
|
63
63
|
### Tool Context
|
|
@@ -118,22 +118,23 @@ async execute(ctx, input) {
|
|
|
118
118
|
}
|
|
119
119
|
```
|
|
120
120
|
|
|
121
|
-
#### `ctx.
|
|
121
|
+
#### `ctx.env`
|
|
122
122
|
|
|
123
|
-
Access to environment variables defined
|
|
123
|
+
Access to parsed environment variables defined by the `Gram` instance:
|
|
124
124
|
|
|
125
125
|
```typescript
|
|
126
|
-
|
|
126
|
+
const gram = new Gram({
|
|
127
|
+
envSchema: {
|
|
128
|
+
BASE_URL: z.string().transform((url) => new URL(url)),
|
|
129
|
+
},
|
|
130
|
+
}).tool({
|
|
127
131
|
name: "api_call",
|
|
128
132
|
inputSchema: { endpoint: z.string() },
|
|
129
|
-
variables: {
|
|
130
|
-
API_KEY: { description: "API key for authentication" }
|
|
131
|
-
},
|
|
132
133
|
async execute(ctx, input) {
|
|
133
|
-
const
|
|
134
|
-
// Use
|
|
134
|
+
const baseURL = ctx.env.BASE_URL;
|
|
135
|
+
// Use baseURL...
|
|
135
136
|
},
|
|
136
|
-
})
|
|
137
|
+
});
|
|
137
138
|
```
|
|
138
139
|
|
|
139
140
|
## Input Validation
|
|
@@ -159,7 +160,8 @@ import * as z from "zod/mini";
|
|
|
159
160
|
|
|
160
161
|
### Lax Mode
|
|
161
162
|
|
|
162
|
-
By default, the framework strictly validates input. You can enable lax mode to
|
|
163
|
+
By default, the framework strictly validates input. You can enable lax mode to
|
|
164
|
+
allow unvalidated input to pass through:
|
|
163
165
|
|
|
164
166
|
```typescript
|
|
165
167
|
const g = new Gram({ lax: true });
|
|
@@ -167,41 +169,48 @@ const g = new Gram({ lax: true });
|
|
|
167
169
|
|
|
168
170
|
## Environment Variables
|
|
169
171
|
|
|
170
|
-
###
|
|
172
|
+
### Defining Variables
|
|
171
173
|
|
|
172
|
-
|
|
174
|
+
Environment variables that are used by tools must be defined when instantiating
|
|
175
|
+
the `Gram` class. This is done using a Zod v4 object schema:
|
|
173
176
|
|
|
174
177
|
```typescript
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
178
|
+
import * as z from "zod/mini";
|
|
179
|
+
|
|
180
|
+
const gram = new Gram({
|
|
181
|
+
envSchema: {
|
|
182
|
+
API_KEY: z.string().describe("API key for external service"),
|
|
183
|
+
BASE_URL: z.string().check(z.url()).describe("Base URL for API requests"),
|
|
179
184
|
},
|
|
180
185
|
});
|
|
181
186
|
```
|
|
182
187
|
|
|
183
|
-
|
|
188
|
+
Whenever a tool wants to access a new environment variable, a definition must be
|
|
189
|
+
added to the `envSchema` if one does not exist. When this Gram Function is
|
|
190
|
+
deployed, end users will then be able to provide values for these variables when
|
|
191
|
+
installing the corresponding MCP servers.
|
|
184
192
|
|
|
185
|
-
###
|
|
193
|
+
### Runtime Environment
|
|
186
194
|
|
|
187
|
-
|
|
195
|
+
Environment variables are read from `process.env` by default, but you can
|
|
196
|
+
override them when creating the `Gram` instance. This can be useful for testing
|
|
197
|
+
or local development. Example:
|
|
188
198
|
|
|
189
199
|
```typescript
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
WEATHER_API_KEY: {
|
|
195
|
-
description: "API key for weather service"
|
|
196
|
-
}
|
|
200
|
+
const g = new Gram({
|
|
201
|
+
envSchema: {
|
|
202
|
+
API_KEY: z.string().describe("API key for external service"),
|
|
203
|
+
BASE_URL: z.string().check(z.url()).describe("Base URL for API requests"),
|
|
197
204
|
},
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
205
|
+
env: {
|
|
206
|
+
API_KEY: "secret-key",
|
|
207
|
+
BASE_URL: "https://api.example.com",
|
|
201
208
|
},
|
|
202
|
-
})
|
|
209
|
+
});
|
|
203
210
|
```
|
|
204
211
|
|
|
212
|
+
If not provided, the framework falls back to `process.env`.
|
|
213
|
+
|
|
205
214
|
## Response Types
|
|
206
215
|
|
|
207
216
|
The framework supports multiple response types. All response methods return Web API `Response` objects.
|
|
@@ -23,7 +23,7 @@ Gram Functions are tools for LLMs and MCP servers that can do arbitrary tasks
|
|
|
23
23
|
such as fetching data from APIs, performing calculations, or interacting with
|
|
24
24
|
hosted databases.
|
|
25
25
|
|
|
26
|
-
##
|
|
26
|
+
## Quick start
|
|
27
27
|
|
|
28
28
|
To get started, install dependencies and run the development server:
|
|
29
29
|
|
|
@@ -37,6 +37,12 @@ To build a zip file that can be deployed to Gram, run:
|
|
|
37
37
|
pnpm build
|
|
38
38
|
```
|
|
39
39
|
|
|
40
|
+
After building, push your function to Gram with:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
pnpm push
|
|
44
|
+
```
|
|
45
|
+
|
|
40
46
|
## Testing Locally
|
|
41
47
|
|
|
42
48
|
If you want to poke at the tools you've built during local development, you can
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# Gram Function MCP Template
|
|
2
|
+
|
|
3
|
+
This template allows you to use the official [MCP TypeScript SDK][mcp-ts] to
|
|
4
|
+
build and deploy [Gram Functions](https://getgram.ai).
|
|
5
|
+
|
|
6
|
+
[mcp-ts]: https://github.com/modelcontextprotocol/typescript-sdk
|
|
7
|
+
|
|
8
|
+
Use Gram Functions to build tools and resources for MCP servers. They can do
|
|
9
|
+
arbitrary tasks such as fetching data from APIs, performing calculations, or
|
|
10
|
+
interacting with hosted databases.
|
|
11
|
+
|
|
12
|
+
## Prerequisites
|
|
13
|
+
|
|
14
|
+
- [Node.js](https://nodejs.org) version 22.18.0 or later
|
|
15
|
+
- [Gram CLI](https://www.speakeasy.com/docs/gram/command-line/use)
|
|
16
|
+
|
|
17
|
+
## Quick start
|
|
18
|
+
|
|
19
|
+
To get started, install dependencies and run the development server:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pnpm install
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
To build a zip file that can be deployed to Gram, run:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pnpm build
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
After building, push your function to Gram with:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pnpm push
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Testing Locally
|
|
38
|
+
|
|
39
|
+
If you want to poke at the tools you've built during local development, you can
|
|
40
|
+
start a local MCP server over stdio transport with:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
pnpm dev
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Specifically, this command will spin up [MCP inspector][mcp-inspector] to let
|
|
47
|
+
you interactively test your tools and resources.
|
|
48
|
+
|
|
49
|
+
[mcp-inspector]: https://github.com/modelcontextprotocol/inspector
|
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { withGram } from "@gram-ai/functions/mcp";
|
|
2
2
|
import { server } from "./mcp.ts";
|
|
3
3
|
|
|
4
|
-
export default
|
|
4
|
+
export default withGram(server, {
|
|
5
|
+
// Describe environment variables required by the function here. These will be
|
|
6
|
+
// available to fill in the Gram dashboard and hosted MCP servers. Example:
|
|
7
|
+
// variables: {
|
|
8
|
+
// API_KEY: { description: "API key for authentication" },
|
|
9
|
+
// },
|
|
10
|
+
});
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@gram-ai/create-function",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.7.0",
|
|
5
5
|
"description": "Build AI tools and deploy them to getgram.ai",
|
|
6
6
|
"keywords": [],
|
|
7
7
|
"homepage": "https://github.com/speakeasy-api/gram",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"prettier": "^3.6.2",
|
|
39
39
|
"typescript": "5.9.2",
|
|
40
40
|
"zod": "^3.25.76",
|
|
41
|
-
"@gram-ai/functions": "^0.
|
|
41
|
+
"@gram-ai/functions": "^0.7.0"
|
|
42
42
|
},
|
|
43
43
|
"scripts": {
|
|
44
44
|
"build": "tsc --noEmit false"
|