Skip to content

Commit cfcec60

Browse files
authored
fix: update README and introspection logic to support schema files hosted at URLs (#32)
1 parent c7b0346 commit cfcec60

File tree

3 files changed

+244
-222
lines changed

3 files changed

+244
-222
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Run `mcp-graphql` with the correct endpoint, it will automatically try to intros
2020
| `HEADERS` | JSON string containing headers for requests | `{}` |
2121
| `ALLOW_MUTATIONS` | Enable mutation operations (disabled by default) | `false` |
2222
| `NAME` | Name of the MCP server | `mcp-graphql` |
23-
| `SCHEMA` | Path to a local GraphQL schema file (optional) | - |
23+
| `SCHEMA` | Path to a local GraphQL schema file or URL (optional) | - |
2424

2525
### Examples
2626

@@ -36,18 +36,21 @@ ENDPOINT=http://localhost:3000/graphql ALLOW_MUTATIONS=true npx mcp-graphql
3636

3737
# Using a local schema file instead of introspection
3838
ENDPOINT=http://localhost:3000/graphql SCHEMA=./schema.graphql npx mcp-graphql
39+
40+
# Using a schema file hosted at a URL
41+
ENDPOINT=http://localhost:3000/graphql SCHEMA=https://example.com/schema.graphql npx mcp-graphql
3942
```
4043

4144
## Resources
4245

43-
- **graphql-schema**: The server exposes the GraphQL schema as a resource that clients can access. This is either the local schema file or based on an introspection query.
46+
- **graphql-schema**: The server exposes the GraphQL schema as a resource that clients can access. This is either the local schema file, a schema file hosted at a URL, or based on an introspection query.
4447

4548
## Available Tools
4649

4750
The server provides two main tools:
4851

4952
1. **introspect-schema**: This tool retrieves the GraphQL schema. Use this first if you don't have access to the schema as a resource.
50-
This uses either the local schema file or an introspection query.
53+
This uses either the local schema file, a schema file hosted at a URL, or an introspection query.
5154

5255
2. **query-graphql**: Execute GraphQL queries against the endpoint. By default, mutations are disabled unless `ALLOW_MUTATIONS` is set to `true`.
5356

src/helpers/introspection.ts

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,53 @@
1-
import { buildClientSchema, getIntrospectionQuery, printSchema } from "graphql";
2-
import { readFile } from "node:fs/promises";
1+
import {buildClientSchema, getIntrospectionQuery, printSchema} from "graphql";
2+
import {readFile} from "node:fs/promises";
3+
34
/**
45
* Introspect a GraphQL endpoint and return the schema as the GraphQL SDL
56
* @param endpoint - The endpoint to introspect
7+
* @param headers - Optional headers to include in the request
68
* @returns The schema
79
*/
810
export async function introspectEndpoint(
9-
endpoint: string,
10-
headers?: Record<string, string>,
11+
endpoint: string,
12+
headers?: Record<string, string>,
1113
) {
12-
const response = await fetch(endpoint, {
13-
method: "POST",
14-
headers: {
15-
"Content-Type": "application/json",
16-
...headers,
17-
},
18-
body: JSON.stringify({
19-
query: getIntrospectionQuery(),
20-
}),
21-
});
22-
23-
if (!response.ok) {
24-
throw new Error(`GraphQL request failed: ${response.statusText}`);
25-
}
26-
27-
const responseJson = await response.json();
28-
// Transform to a schema object
29-
const schema = buildClientSchema(responseJson.data);
30-
31-
// Print the schema SDL
32-
return printSchema(schema);
14+
const response = await fetch(endpoint, {
15+
method: "POST",
16+
headers: {
17+
"Content-Type": "application/json",
18+
...headers,
19+
},
20+
body: JSON.stringify({
21+
query: getIntrospectionQuery(),
22+
}),
23+
});
24+
25+
if (!response.ok) {
26+
throw new Error(`GraphQL request failed: ${response.statusText}`);
27+
}
28+
29+
const responseJson = await response.json();
30+
// Transform to a schema object
31+
const schema = buildClientSchema(responseJson.data);
32+
33+
// Print the schema SDL
34+
return printSchema(schema);
35+
}
36+
37+
/**
38+
* Introspect a GraphQL schema file hosted at a URL and return the schema as the GraphQL SDL
39+
* @param url - The URL to the schema file
40+
* @returns The schema
41+
*/
42+
export async function introspectSchemaFromUrl(url: string) {
43+
const response = await fetch(url);
44+
45+
if (!response.ok) {
46+
throw new Error(`Failed to fetch schema from URL: ${response.statusText}`);
47+
}
48+
49+
const schema = await response.text();
50+
return schema;
3351
}
3452

3553
/**
@@ -38,6 +56,6 @@ export async function introspectEndpoint(
3856
* @returns The schema
3957
*/
4058
export async function introspectLocalSchema(path: string) {
41-
const schema = await readFile(path, "utf8");
42-
return schema;
59+
const schema = await readFile(path, "utf8");
60+
return schema;
4361
}

0 commit comments

Comments
 (0)