You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -12,7 +12,53 @@ A Model Context Protocol (MCP) server that allows LLMs to interact with Subgraph
12
12
- Supports MCP resources, tools, and prompts
13
13
- Can run in STDIO mode or as an SSE (Server-Sent Events) server
14
14
15
-
## Requirements
15
+
## Usage
16
+
17
+
The `subgraph-mcp` server offers two primary ways to interact with The Graph Network:
18
+
19
+
1.**Connecting to the Remote Hosted MCP Service (Recommended for most users)**
20
+
2.**Building and Running the Server Locally**
21
+
22
+
### Connecting to the Remote Hosted MCP Service
23
+
24
+
This is the quickest way to get started. You can configure your MCP client (e.g., Claude Desktop) to connect to our hosted `subgraph-mcp` service.
25
+
26
+
**Requirements:**
27
+
28
+
- A Gateway API key for The Graph Network.
29
+
30
+
**Configuration:**
31
+
32
+
Add the following to your configuration file of your client (e.g.,`claude_desktop_config.json`):
33
+
34
+
```json
35
+
{
36
+
"mcpServers": {
37
+
"subgraph-mcp": {
38
+
"command": "npx",
39
+
"args": [
40
+
"mcp-remote",
41
+
"--header",
42
+
"Authorization:${AUTH_HEADER}",
43
+
"https://subgraphs.mcp.thegraph.com/sse"
44
+
],
45
+
"env": {
46
+
"AUTH_HEADER": "Bearer YOUR_GATEWAY_API_KEY"// <-- Replace with your actual key
47
+
}
48
+
}
49
+
}
50
+
}
51
+
```
52
+
53
+
Replace `YOUR_GATEWAY_API_KEY` with your actual Gateway API key. After adding the configuration, restart your MCP client.
54
+
55
+
Once configured, you can skip to the "Available Tools" or "Natural Language Queries" sections to learn how to interact with the service.
56
+
57
+
### Building and Running the Server Locally
58
+
59
+
This option is for users who prefer to build, run, and potentially modify the server on their own machine.
60
+
61
+
#### Requirements (for Local Execution)
16
62
17
63
- Rust (latest stable version recommended: 1.75+). \
18
64
You can install it using the following command on macOS, Linux, or other Unix-like systems: \
@@ -21,9 +67,8 @@ A Model Context Protocol (MCP) server that allows LLMs to interact with Subgraph
21
67
```
22
68
Follow the on-screen instructions. For other platforms, see the [official Rust installation guide](https://www.rust-lang.org/tools/install).
23
69
- A Gateway API key for The Graph Network.
24
-
- For Claude Desktop users: Latest Claude Desktop version
25
70
26
-
## Installation
71
+
####Installation (for Local Execution)
27
72
28
73
```bash
29
74
# Clone the repository
@@ -34,94 +79,74 @@ cd subgraph-mcp
34
79
cargo build --release
35
80
```
36
81
37
-
## Usage
38
-
39
-
The server can be run in two modes: STDIO or SSE.
40
-
41
-
### STDIO Mode (e.g., for Claude Desktop)
42
-
43
-
This mode is typically used for direct integration with local MCP clients like Claude Desktop.
82
+
#### Configuration (for Local Execution)
44
83
45
-
#### Configuration with Claude Desktop
46
-
47
-
Add the server to your `claude_desktop_config.json`. You need to replace `/path/to/subgraph-mcp` with the **absolute path** to the compiled binary you built in the Installation step.
48
-
49
-
**Finding the command path:**
50
-
After running `cargo build --release`, the executable will typically be located at `target/release/subgraph-mcp` inside your project directory (`subgraph-mcp`).
51
-
52
-
1. Navigate to your `subgraph-mcp` directory in the terminal.
53
-
2. Run `pwd` (print working directory) to get the full path to the `subgraph-mcp` directory.
54
-
3. Combine the output of `pwd` with `/target/release/subgraph-mcp`.
55
-
56
-
For example, if `pwd` outputs `/Users/user/subgraph-mcp`, the full command path would be `/Users/user/subgraph-mcp/target/release/subgraph-mcp`.
57
-
58
-
**Configuration Example:**
84
+
Add the following to your configuration file of your client (e.g.,`claude_desktop_config.json`):
59
85
60
86
```json
61
87
{
62
88
"mcpServers": {
63
-
"subgraph": {
89
+
"subgraph-mcp": {
64
90
"command": "/path/to/your/subgraph-mcp/target/release/subgraph-mcp", // <-- Replace this with the actual path!
65
91
"env": {
66
-
"GATEWAY_API_KEY": "your-api-key-here"
92
+
"GATEWAY_API_KEY": "YOUR_GATEWAY_API_KEY"// <-- Replace with your actual key
67
93
}
68
94
}
69
95
}
70
96
}
71
97
```
72
98
99
+
You need to replace `/path/to/subgraph-mcp` with the **absolute path** to the compiled binary you built in the Installation step.
100
+
101
+
**Finding the command path:**
102
+
After running `cargo build --release`, the executable will typically be located at `target/release/subgraph-mcp` inside your project directory (`subgraph-mcp`).
103
+
104
+
1. Navigate to your `subgraph-mcp` directory in the terminal.
105
+
2. Run `pwd` (print working directory) to get the full path to the `subgraph-mcp` directory.
106
+
3. Combine the output of `pwd` with `/target/release/subgraph-mcp`.
107
+
108
+
For example, if `pwd` outputs `/Users/user/subgraph-mcp`, the full command path would be `/Users/user/subgraph-mcp/target/release/subgraph-mcp`.
109
+
73
110
After adding the configuration, restart Claude Desktop.
74
111
75
-
**Important**: Claude Desktop may not automatically utilize server resources. To ensure proper functionality, manually add "Subgraph MCP LLM Guidence" resource to your chat context by clicking on the context menu and adding the resource`graphql://subgraph`.
112
+
**Important**: Claude Desktop may not automatically utilize server resources. To ensure proper functionality, manually add `Subgraph Server Instructions`resource to your chat context by clicking on the context menu and adding the resource.
76
113
77
114
## Available Tools
78
115
79
116
The server exposes the following tools:
80
117
81
-
-**`search_subgraphs_by_keyword`**: Search for subgraphs by keyword in their display names. Ordered by signal. Returns top 10 results if total results ≤ 100, or square root of total otherwise. (Corresponds to Step 2 of the workflow).
82
-
-**`get_deployment_30day_query_counts`**: Get the aggregate query count over the last 30 days for multiple subgraph deployments (using their IPFS hashes), sorted by query count. (Corresponds to Step 3 of the workflow).
118
+
-**`search_subgraphs_by_keyword`**: Search for subgraphs by keyword in their display names. Ordered by signal. Returns top 10 results if total results ≤ 100, or square root of total otherwise.
119
+
-**`get_deployment_30day_query_counts`**: Get the aggregate query count over the last 30 days for multiple subgraph deployments (using their IPFS hashes), sorted by query count.
83
120
-**`get_schema_by_deployment_id`**: Get the GraphQL schema for a specific subgraph deployment using its _deployment ID_ (e.g., `0x...`).
84
121
-**`get_schema_by_subgraph_id`**: Get the GraphQL schema for the _current_ deployment associated with a _subgraph ID_ (e.g., `5zvR82...`).
85
122
-**`get_schema_by_ipfs_hash`**: Get the GraphQL schema for a specific subgraph deployment using its manifest's _IPFS hash_ (e.g., `Qm...`).
86
123
-**`execute_query_by_deployment_id`**: Execute a GraphQL query against a specific, immutable subgraph deployment using its _deployment ID_ (e.g., `0x...`).
87
124
-**`execute_query_by_subgraph_id`**: Execute a GraphQL query against the _latest_ deployment associated with a _subgraph ID_ (e.g., `5zvR82...`).
88
125
-**`execute_query_by_ipfs_hash`**: Execute a GraphQL query against a specific, immutable subgraph deployment using its _IPFS hash_ (e.g., `Qm...`).
89
-
-**`get_top_subgraph_deployments`**: Get the top 3 subgraph deployments indexing a given contract address on a specific chain, ordered by query fees. (Used in the "Contract Address Lookup" special case).
90
-
91
-
**Key Identifier Types:**
126
+
-**`get_top_subgraph_deployments`**: Get the top 3 subgraph deployments indexing a given contract address on a specific chain, ordered by query fees.
92
127
93
-
-**Subgraph ID** (e.g., `5zvR82...`): Logical identifier for a subgraph. Use `execute_query_by_subgraph_id` or `get_schema_by_subgraph_id`.
94
-
-**Deployment ID** (e.g., `0x4d7c...`): Identifier for a specific, immutable deployment. Use `execute_query_by_deployment_id` or `get_schema_by_deployment_id`.
95
-
-**IPFS Hash** (e.g., `QmTZ8e...`): Identifier for the manifest of a specific, immutable deployment. Use `execute_query_by_ipfs_hash` or `get_schema_by_ipfs_hash`.
128
+
### Natural Language Queries
96
129
97
-
Example usage in Claude (or other MCP clients), keeping the workflow in mind:
130
+
Once connected to an LLM with this MCP server, you can ask natural language questions.
98
131
99
-
```
100
-
User: Find subgraphs for Uniswap.
132
+
**Important**: Claude Desktop may not automatically utilize server resources. To ensure proper functionality, manually add `Subgraph Server Instructions` resource to your chat context by clicking on the context menu and adding the resource.
101
133
102
-
Assistant (after `search_subgraphs_by_keyword` and `get_deployment_30day_query_counts`):
103
-
I found several Uniswap subgraphs.
104
-
- Uniswap v3 on Ethereum is the most active (X queries last 30 days).
105
-
- Uniswap v2 on Ethereum (Y queries last 30 days).
106
-
- Uniswap v3 on Arbitrum (Z queries last 30 days).
107
-
Which specific version and network are you interested in?
134
+
Example usage in Claude (or other MCP clients), assuming you added `Subgraph Server Instructions` to your prompt:
108
135
109
-
Find the top subgraphs for contract 0x1f98431c8ad98523631ae4a59f267346ea31f984 on arbitrum-one
110
136
```
137
+
User: List the 20 most recently registered .eth names.
111
138
112
-
### Natural Language Queries (Following the Workflow)
113
-
114
-
Once connected to an LLM with this MCP server, you can ask natural language questions. The LLM should adhere to the `SERVER_INSTRUCTIONS` workflow:
139
+
Assistant (after `search_subgraphs_by_keyword`, `get_deployment_30day_query_counts` and other tool usage):
140
+
Perfect! I've successfully retrieved the 20 most recently registered .eth names using the ENS subgraph, which has 68.1 million queries in the last 30 days, making it the most active and reliable source for ENS data.
141
+
Here are the 20 most recently registered .eth names:
115
142
116
-
```
117
-
What are the most active pools on Uniswap v3 on Ethereum?
143
+
...
118
144
119
-
(LLM internally performs search, query volume check, selects subgraph, potentially gets schema, then formulates and executes the query)
120
145
```
121
146
122
147
The LLM will automatically:
123
148
124
-
1. Follow the **Server Instructions & Workflow** described above.
149
+
1. Follow the **Subgraph Server Instructions**.
125
150
2. Use `search_subgraphs_by_keyword` to find candidate subgraphs.
126
151
3. Use `get_deployment_30day_query_counts` to verify activity and aid selection.
127
152
4. Use `get_top_subgraph_deployments` if a contract address is provided.
@@ -146,7 +171,68 @@ The server provides predefined prompts for most tools (as discoverable via MCP's
146
171
147
172
The server exposes one resource:
148
173
149
-
-`graphql://subgraph`: Provides the detailed `SERVER_INSTRUCTIONS` used by the LLM, including the workflow for different user goals (address lookup, finding subgraphs for a contract, querying by ID, getting schema) and important usage notes.
174
+
-`graphql://subgraph`: Provides the detailed `Subgraph Server Instructions` used by the LLM, including the workflow for different user goals (address lookup, finding subgraphs for a contract, querying by ID, getting schema) and important usage notes.
175
+
176
+
Below is a reference for the `Subgraph Server Instructions`:
177
+
178
+
```
179
+
**Interacting with The Graph Subgraphs**
180
+
**IMPORTANT: ALWAYS verify query volumes using `get_deployment_30day_query_counts` for any potential subgraph candidate *before* selecting or querying it. This step is NON-OPTIONAL. Failure to do so may result in using outdated or irrelevant data.**
181
+
**Follow this sequence strictly:**
182
+
1. **Analyze User Request:**
183
+
* Identify the **protocol name** (e.g., "Uniswap", "Aave", "ENS").
184
+
* Note any specific **version** or **blockchain network** mentioned by the user.
185
+
* Determine the **goal**: Query data? Get schema?
186
+
2. **Initial Search & Preliminary Analysis:**
187
+
* Use `search_subgraphs_by_keyword` with the most generic term for the protocol (e.g., if "Uniswap v3 on Ethereum", initially search only for "Uniswap").
188
+
* Examine `displayName` and other metadata in the search results for version and network information.
* **ALWAYS** extract the IPFS hashes (`ipfsHash`) for all potentially relevant subgraphs identified in Step 2.
191
+
* **ALWAYS** use `get_deployment_30day_query_counts` for these IPFS hashes.
192
+
* **If Ambiguous (Multiple Versions/Chains with significant volume):**
193
+
* Present a summary to the user, **including the 30-day query counts for each option**. For example: "I found several Uniswap subgraphs. Uniswap v3 on Ethereum is the most active (X queries last 30 days). I also see Uniswap v2 on Ethereum (Y queries) and Uniswap v3 on Arbitrum (Z queries). Which specific version and network are you interested in?"
194
+
* **If Still Unclear (Information Missing and Not Inferable even with query volumes):**
195
+
* If version/chain information is genuinely missing from search results and user input, and query volumes don't offer a clear path (e.g. all relevant subgraphs have very low or no volume), ask for clarification directly. Example: "I found several subgraphs for 'ExampleProtocol', but none have significant query activity. Could you please specify the version and blockchain network you're interested in?"
196
+
* **Do NOT proceed to Step 4 without completing this query volume verification.**
197
+
4. **Select Final Subgraph (Post Query Volume Check & Clarification):**
198
+
* After the keyword search, mandatory query volume check, and any necessary clarification, you should have a clear target protocol, version, and network.
199
+
* Identify all candidate subgraphs from your Step 2 `search_subgraphs_by_keyword` results that match these clarified criteria.
200
+
* **If there is more than one such matching subgraph:**
201
+
* You should have already fetched their query counts in Step 3.
202
+
* **Select the subgraph with the highest `total_query_count`** among them.
203
+
* **If only one subgraph precisely matches the criteria**, that is your selected subgraph.
204
+
* When presenting your chosen subgraph or asking for final confirmation before querying, **ALWAYS state its 30-day query volume** to demonstrate this check has been performed. For example: "I've selected the 'Uniswap v3 Ethereum' subgraph, which has X queries in the last 30 days. Shall I proceed to get its schema?"
205
+
* If the selected subgraph's query count is very low (and this wasn't already discussed during clarification), briefly inform the user.
206
+
5. **Execute Action Using the Identified Subgraph:**
207
+
* **Identify the ID Type:** (Subgraph ID, Deployment ID, or IPFS Hash - note that `search_subgraphs_by_keyword` returns `id` for Subgraph ID and `ipfsHash` for current deployment's IPFS hash).
208
+
* **Determine the Correct Tool based on Goal & ID Type:**
209
+
* **Goal: Query Data**
210
+
* Subgraph ID (`id` from search) → `execute_query_by_subgraph_id`
211
+
* Deployment ID (0x...) → `execute_query_by_deployment_id`
212
+
* IPFS Hash (`ipfsHash` from search) → `execute_query_by_ipfs_hash`
213
+
* **Goal: Get Schema**
214
+
* Subgraph ID → `get_schema_by_subgraph_id`
215
+
* Deployment ID → `get_schema_by_deployment_id`
216
+
* IPFS Hash → `get_schema_by_ipfs_hash`
217
+
* **Write Clean GraphQL Queries:** Simple structure, omit 'variables' if unused, include only essential fields.
218
+
**Special Case: Contract Address Lookup**
219
+
* ONLY when a user explicitly provides a **contract address** (0x...) AND asks for subgraphs related to it:
220
+
* Identify the blockchain network for the address (ask user if unclear).
221
+
* Use `get_top_subgraph_deployments` with the provided contract address and chain name.
222
+
* Process and use the resulting IPFS hashes as needed. **Crucially, before using any of these IPFS hashes for querying, first use `get_deployment_30day_query_counts` with their IPFS hashes to verify recent activity.**
223
+
**ID Type Reference:**
224
+
* **Subgraph ID**: Typically starts with digits and letters (e.g., 5zvR82...)
225
+
* **Contract Address**: A shorter hexadecimal string, typically 42 characters long including the "0x" prefix (e.g., 0x1a3c9b1d2f0529d97f2afc5136cc23e58f1fd35b).
226
+
* **Deployment ID**: A longer hexadecimal string, typically 66 characters long including the "0x" prefix (e.g., 0xc5b4d246cf890b0b468e005224622d4c85a8b723cc0b8fa7db6d1a93ddd2e5de). Use length to distinguish from a Contract Address.
227
+
* **IPFS Hash**: Typically starts with Qm... For the purpose of `get_deployment_30day_query_counts`, use the \'IPFS Hash\' (Qm...).
228
+
* Note `search_subgraphs_by_keyword` and `get_top_subgraph_deployments` returns `ipfsHash`.
229
+
230
+
**Best Practices:**
231
+
* When using GraphQL, if unsure about the structure, first get the schema to understand available entities and fields.
232
+
* Create focused queries that only request necessary fields.
233
+
* For paginated data, use appropriate limit parameters.
pubconstSERVER_INSTRUCTIONS:&str = "**Interacting with The Graph Subgraphs**
6
+
pubconstSUBGRAPH_SERVER_INSTRUCTIONS:&str = "**Interacting with The Graph Subgraphs**
7
7
**IMPORTANT: ALWAYS verify query volumes using `get_deployment_30day_query_counts` for any potential subgraph candidate *before* selecting or querying it. This step is NON-OPTIONAL. Failure to do so may result in using outdated or irrelevant data.**
0 commit comments