graphql-agent-toolkit 1.0.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/LICENSE +21 -0
- package/README.md +369 -0
- package/dist/cli.cjs +574 -0
- package/dist/cli.cjs.map +1 -0
- package/dist/cli.d.cts +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +573 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.cjs +1726 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +260 -0
- package/dist/index.d.ts +260 -0
- package/dist/index.js +1683 -0
- package/dist/index.js.map +1 -0
- package/package.json +85 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mark Stuart
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
# graphql-agent-toolkit
|
|
2
|
+
|
|
3
|
+
[](https://github.com/mstuart/graphql-agent-toolkit/actions/workflows/ci.yml)
|
|
4
|
+
[](https://www.npmjs.com/package/graphql-agent-toolkit)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
|
|
7
|
+
Turn any GraphQL API into AI-agent-ready tools -- MCP servers, LangChain tools, and framework adapters.
|
|
8
|
+
|
|
9
|
+
**graphql-agent-toolkit** introspects a GraphQL endpoint, generates typed operations, and exposes them as tools that AI agents can discover and call. It supports the [Model Context Protocol (MCP)](https://modelcontextprotocol.io/) out of the box, so you can connect any MCP-compatible AI client to any GraphQL API in seconds.
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npx graphql-agent-toolkit init --endpoint https://your-api.com/graphql
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
This introspects your schema and prints a configuration summary. To start an MCP server:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npx graphql-agent-toolkit serve --endpoint https://your-api.com/graphql
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install graphql-agent-toolkit graphql
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Requirements
|
|
30
|
+
|
|
31
|
+
- Node.js >= 18.0.0
|
|
32
|
+
- `graphql` >= 16.0.0 (peer dependency)
|
|
33
|
+
- TypeScript >= 5.0 (optional, for type definitions)
|
|
34
|
+
|
|
35
|
+
Fully written in TypeScript with complete type exports for all public APIs.
|
|
36
|
+
|
|
37
|
+
## Features
|
|
38
|
+
|
|
39
|
+
- **Schema Introspection** -- Automatically fetches and parses any GraphQL schema
|
|
40
|
+
- **Operation Builder** -- Generates queries and mutations with proper variable definitions and nested selection sets
|
|
41
|
+
- **MCP Server** -- Creates a fully functional MCP server with tools for every query and mutation
|
|
42
|
+
- **Semantic Search** -- TF-IDF powered schema navigator to find relevant types and fields
|
|
43
|
+
- **Pagination Handling** -- Auto-detects and handles Relay and offset pagination across multiple pages
|
|
44
|
+
- **Result Summarization** -- Truncate large responses for LLM context windows with markdown formatting
|
|
45
|
+
- **Framework Adapters** -- Generate tools for LangChain, CrewAI, and Vercel AI SDK with zero framework dependencies
|
|
46
|
+
- **Mock Data Generation** -- Generate deterministic mock data from your schema with `@mock()` directive support
|
|
47
|
+
- **CLI** -- Command-line interface for quick setup and serving
|
|
48
|
+
- **Dual Format** -- Ships as both ESM and CJS with full TypeScript types
|
|
49
|
+
|
|
50
|
+
## Programmatic API
|
|
51
|
+
|
|
52
|
+
### Introspect and Parse a Schema
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
import { fetchSchema, parseSchema } from 'graphql-agent-toolkit';
|
|
56
|
+
|
|
57
|
+
const introspection = await fetchSchema({
|
|
58
|
+
endpoint: 'https://your-api.com/graphql',
|
|
59
|
+
headers: { Authorization: 'Bearer YOUR_TOKEN' },
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const schema = parseSchema(introspection);
|
|
63
|
+
|
|
64
|
+
console.log(`Query type: ${schema.queryType}`);
|
|
65
|
+
console.log(`Types: ${schema.types.size}`);
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Build Operations
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
import { fetchSchema, parseSchema, buildOperation } from 'graphql-agent-toolkit';
|
|
72
|
+
|
|
73
|
+
const introspection = await fetchSchema({ endpoint: 'https://your-api.com/graphql' });
|
|
74
|
+
const schema = parseSchema(introspection);
|
|
75
|
+
|
|
76
|
+
const op = buildOperation(schema, 'user', { maxDepth: 3 });
|
|
77
|
+
console.log(op.operation);
|
|
78
|
+
// query UserQuery($id: ID!) {
|
|
79
|
+
// user(id: $id) {
|
|
80
|
+
// id
|
|
81
|
+
// name
|
|
82
|
+
// email
|
|
83
|
+
// posts {
|
|
84
|
+
// id
|
|
85
|
+
// title
|
|
86
|
+
// }
|
|
87
|
+
// }
|
|
88
|
+
// }
|
|
89
|
+
console.log(op.variables);
|
|
90
|
+
// [{ name: 'id', type: 'ID!', required: true, description: 'User ID' }]
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Create an MCP Server
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
import { createAgentToolkitServer } from 'graphql-agent-toolkit';
|
|
97
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
98
|
+
|
|
99
|
+
const server = await createAgentToolkitServer({
|
|
100
|
+
endpoint: 'https://your-api.com/graphql',
|
|
101
|
+
headers: { Authorization: 'Bearer YOUR_TOKEN' },
|
|
102
|
+
operationDepth: 2,
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
const transport = new StdioServerTransport();
|
|
106
|
+
await server.connect(transport);
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Each query becomes a `query_<fieldName>` tool, and each mutation becomes a `mutate_<fieldName>` tool. An additional `explore_schema` tool lets the agent browse types and fields.
|
|
110
|
+
|
|
111
|
+
### Semantic Schema Navigation
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
import { fetchSchema, parseSchema, SchemaNavigator } from 'graphql-agent-toolkit';
|
|
115
|
+
|
|
116
|
+
const introspection = await fetchSchema({ endpoint: 'https://your-api.com/graphql' });
|
|
117
|
+
const schema = parseSchema(introspection);
|
|
118
|
+
|
|
119
|
+
const navigator = new SchemaNavigator();
|
|
120
|
+
navigator.index(schema);
|
|
121
|
+
|
|
122
|
+
// Search for relevant types
|
|
123
|
+
const results = navigator.search('user authentication');
|
|
124
|
+
for (const result of results) {
|
|
125
|
+
console.log(`${result.typeName} (${result.kind}) - score: ${result.score.toFixed(3)}`);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Get detailed context for a type
|
|
129
|
+
const context = navigator.getTypeContext('User');
|
|
130
|
+
console.log(context);
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### Result Summarization
|
|
134
|
+
|
|
135
|
+
Truncate large GraphQL responses to fit within LLM context windows:
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
import { summarizeResponse, formatForLLM } from 'graphql-agent-toolkit';
|
|
139
|
+
|
|
140
|
+
// Summarize a large response
|
|
141
|
+
const { summary, metadata } = summarizeResponse(largeResponse, {
|
|
142
|
+
maxItems: 5, // max array items to include
|
|
143
|
+
maxDepth: 3, // max nesting depth
|
|
144
|
+
maxStringLength: 200, // truncate long strings
|
|
145
|
+
includeMetadata: true, // add _meta with counts
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
console.log(metadata);
|
|
149
|
+
// { totalItems: 1500, truncated: true, originalSize: 48230 }
|
|
150
|
+
|
|
151
|
+
// Format as clean markdown for LLM context
|
|
152
|
+
const markdown = formatForLLM(largeResponse, { maxItems: 10 });
|
|
153
|
+
console.log(markdown);
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Framework Adapters
|
|
157
|
+
|
|
158
|
+
Generate tools for popular AI frameworks -- no framework dependencies required.
|
|
159
|
+
|
|
160
|
+
#### LangChain
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
import { createLangChainTools, createStructuredTools } from 'graphql-agent-toolkit';
|
|
164
|
+
|
|
165
|
+
// Basic tools (input is JSON string)
|
|
166
|
+
const tools = createLangChainTools(schema, executor, { maxDepth: 2 });
|
|
167
|
+
|
|
168
|
+
// Structured tools with Zod schemas (for @langchain/core StructuredTool)
|
|
169
|
+
const structuredTools = createStructuredTools(schema, executor);
|
|
170
|
+
|
|
171
|
+
for (const tool of tools) {
|
|
172
|
+
console.log(`${tool.name}: ${tool.description}`);
|
|
173
|
+
// tool.func(jsonString) -> Promise<string>
|
|
174
|
+
}
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
#### CrewAI
|
|
178
|
+
|
|
179
|
+
```typescript
|
|
180
|
+
import { createCrewAITools } from 'graphql-agent-toolkit';
|
|
181
|
+
|
|
182
|
+
const tools = createCrewAITools(schema, executor);
|
|
183
|
+
|
|
184
|
+
for (const tool of tools) {
|
|
185
|
+
console.log(`${tool.name}: ${tool.description}`);
|
|
186
|
+
// tool.args_schema is a JSON Schema object
|
|
187
|
+
// tool.func(argsObject) -> Promise<string>
|
|
188
|
+
}
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
#### Vercel AI SDK
|
|
192
|
+
|
|
193
|
+
```typescript
|
|
194
|
+
import { createVercelAITools } from 'graphql-agent-toolkit';
|
|
195
|
+
|
|
196
|
+
const tools = createVercelAITools(schema, executor);
|
|
197
|
+
|
|
198
|
+
// Returns Record<string, { description, parameters: ZodSchema, execute }>
|
|
199
|
+
// Use directly with Vercel AI SDK's tool() function
|
|
200
|
+
for (const [name, tool] of Object.entries(tools)) {
|
|
201
|
+
console.log(`${name}: ${tool.description}`);
|
|
202
|
+
// tool.parameters is a Zod schema
|
|
203
|
+
// tool.execute(args) -> Promise<string>
|
|
204
|
+
}
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### Mock Data Generation
|
|
208
|
+
|
|
209
|
+
Generate deterministic mock data from your schema for testing:
|
|
210
|
+
|
|
211
|
+
```typescript
|
|
212
|
+
import { generateMockData, createMockExecutor } from 'graphql-agent-toolkit';
|
|
213
|
+
|
|
214
|
+
// Generate mock data for a specific type
|
|
215
|
+
const mockUser = generateMockData(schema, 'User', {
|
|
216
|
+
seed: 42, // deterministic output
|
|
217
|
+
arrayLength: 3, // items per list field
|
|
218
|
+
maxDepth: 3, // max recursion depth
|
|
219
|
+
});
|
|
220
|
+
console.log(mockUser);
|
|
221
|
+
// { id: 'id_id_0', name: 'mock_name', posts: [...] }
|
|
222
|
+
|
|
223
|
+
// Create a drop-in mock executor (no HTTP calls)
|
|
224
|
+
const mockExecutor = createMockExecutor(schema, { seed: 42 });
|
|
225
|
+
|
|
226
|
+
// Use it anywhere a GraphQLExecutor is expected
|
|
227
|
+
const result = await mockExecutor.execute(
|
|
228
|
+
'query { user(id: "1") { id name } }',
|
|
229
|
+
{ id: '1' }
|
|
230
|
+
);
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
Use the `@mock()` directive in field descriptions for custom values:
|
|
234
|
+
|
|
235
|
+
```graphql
|
|
236
|
+
type Product {
|
|
237
|
+
"The product name @mock(\"Widget Pro\")"
|
|
238
|
+
name: String!
|
|
239
|
+
"Current price in USD @mock(29.99)"
|
|
240
|
+
price: Float!
|
|
241
|
+
"Whether the product is in stock @mock(true)"
|
|
242
|
+
inStock: Boolean!
|
|
243
|
+
}
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
## CLI Usage
|
|
247
|
+
|
|
248
|
+
### `init` -- Introspect and generate config
|
|
249
|
+
|
|
250
|
+
```bash
|
|
251
|
+
graphql-agent-toolkit init \
|
|
252
|
+
--endpoint https://your-api.com/graphql \
|
|
253
|
+
--header "Authorization: Bearer YOUR_TOKEN" \
|
|
254
|
+
--output config.json
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
### `serve` -- Start MCP server
|
|
258
|
+
|
|
259
|
+
```bash
|
|
260
|
+
# From a config file
|
|
261
|
+
graphql-agent-toolkit serve --config config.json
|
|
262
|
+
|
|
263
|
+
# Directly from an endpoint
|
|
264
|
+
graphql-agent-toolkit serve --endpoint https://your-api.com/graphql
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
## MCP Server Usage
|
|
268
|
+
|
|
269
|
+
Add to your MCP client configuration (e.g., Claude Desktop):
|
|
270
|
+
|
|
271
|
+
```json
|
|
272
|
+
{
|
|
273
|
+
"mcpServers": {
|
|
274
|
+
"my-graphql-api": {
|
|
275
|
+
"command": "npx",
|
|
276
|
+
"args": [
|
|
277
|
+
"graphql-agent-toolkit",
|
|
278
|
+
"serve",
|
|
279
|
+
"--endpoint",
|
|
280
|
+
"https://your-api.com/graphql"
|
|
281
|
+
]
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
## Configuration
|
|
288
|
+
|
|
289
|
+
The `AgentToolkitConfig` object accepts:
|
|
290
|
+
|
|
291
|
+
| Property | Type | Default | Description |
|
|
292
|
+
|----------|------|---------|-------------|
|
|
293
|
+
| `endpoint` | `string` | (required) | GraphQL endpoint URL |
|
|
294
|
+
| `headers` | `Record<string, string>` | `{}` | HTTP headers for requests |
|
|
295
|
+
| `operationDepth` | `number` | `2` | Max depth for generated selection sets |
|
|
296
|
+
| `includeDeprecated` | `boolean` | `false` | Include deprecated fields |
|
|
297
|
+
|
|
298
|
+
## API Reference
|
|
299
|
+
|
|
300
|
+
### Introspection
|
|
301
|
+
|
|
302
|
+
- `fetchSchema(options)` -- Fetch introspection query result from a GraphQL endpoint
|
|
303
|
+
- `parseSchema(introspection)` -- Parse raw introspection result into a `ParsedSchema`
|
|
304
|
+
|
|
305
|
+
### Operations
|
|
306
|
+
|
|
307
|
+
- `buildOperation(schema, fieldName, options?)` -- Generate a GraphQL operation string with variables
|
|
308
|
+
|
|
309
|
+
### MCP
|
|
310
|
+
|
|
311
|
+
- `createAgentToolkitServer(config, options?)` -- Create a fully configured MCP server
|
|
312
|
+
- `createToolsFromSchema(schema, executor, options?)` -- Create tool definitions from a parsed schema
|
|
313
|
+
- `GraphQLExecutor` -- Class for executing GraphQL operations
|
|
314
|
+
|
|
315
|
+
### Semantic
|
|
316
|
+
|
|
317
|
+
- `SchemaNavigator` -- Class for indexing and searching a GraphQL schema
|
|
318
|
+
- `.index(schema)` -- Index a parsed schema
|
|
319
|
+
- `.search(query, limit?)` -- Search for relevant types
|
|
320
|
+
- `.getTypeContext(typeName)` -- Get formatted context for a type
|
|
321
|
+
|
|
322
|
+
### Pagination
|
|
323
|
+
|
|
324
|
+
- `executePaginated(executor, operation, variables, config?)` -- Execute a paginated query, collecting all pages
|
|
325
|
+
- `detectPaginationStyle(schema, typeName)` -- Auto-detect Relay or offset pagination from a type
|
|
326
|
+
|
|
327
|
+
### Summarization
|
|
328
|
+
|
|
329
|
+
- `summarizeResponse(data, config?)` -- Truncate arrays, limit depth, and shorten strings in a response
|
|
330
|
+
- `formatForLLM(data, config?)` -- Format data as clean markdown for LLM context
|
|
331
|
+
|
|
332
|
+
### Framework Adapters
|
|
333
|
+
|
|
334
|
+
- `createLangChainTools(schema, executor, options?)` -- Create LangChain-compatible tools (JSON string input)
|
|
335
|
+
- `createStructuredTools(schema, executor, options?)` -- Create LangChain StructuredTool-compatible tools (Zod schemas)
|
|
336
|
+
- `createCrewAITools(schema, executor, options?)` -- Create CrewAI-compatible tools (dict input, `args_schema`)
|
|
337
|
+
- `createVercelAITools(schema, executor, options?)` -- Create Vercel AI SDK-compatible tools (Zod parameters, Record)
|
|
338
|
+
|
|
339
|
+
### Mock Data
|
|
340
|
+
|
|
341
|
+
- `generateMockData(schema, typeName, config?)` -- Generate mock data for a given type
|
|
342
|
+
- `createMockExecutor(schema, config?)` -- Create a mock executor as drop-in replacement for GraphQLExecutor
|
|
343
|
+
|
|
344
|
+
### Types
|
|
345
|
+
|
|
346
|
+
- `AgentToolkitConfig` -- Configuration object
|
|
347
|
+
- `ParsedSchema` -- Parsed schema with type map
|
|
348
|
+
- `SchemaType` -- Individual type definition
|
|
349
|
+
- `SchemaField` -- Field definition with args
|
|
350
|
+
- `GeneratedOperation` -- Generated operation with variables
|
|
351
|
+
- `SearchResult` -- Semantic search result
|
|
352
|
+
- `SummaryConfig` -- Configuration for response summarization
|
|
353
|
+
- `PaginationConfig` -- Configuration for paginated queries
|
|
354
|
+
- `MockConfig` -- Configuration for mock data generation
|
|
355
|
+
- `LangChainToolConfig` -- LangChain tool definition shape
|
|
356
|
+
- `CrewAIToolConfig` -- CrewAI tool definition shape
|
|
357
|
+
- `VercelAIToolConfig` -- Vercel AI SDK tool definition shape
|
|
358
|
+
|
|
359
|
+
## Contributing
|
|
360
|
+
|
|
361
|
+
1. Clone the repository
|
|
362
|
+
2. Install dependencies: `npm install`
|
|
363
|
+
3. Run tests: `npm test`
|
|
364
|
+
4. Build: `npm run build`
|
|
365
|
+
5. Lint: `npm run lint`
|
|
366
|
+
|
|
367
|
+
## License
|
|
368
|
+
|
|
369
|
+
MIT
|