|
| 1 | +import { ActionProvider, WalletProvider } from "@coinbase/agentkit"; |
| 2 | +import { z } from "zod"; |
| 3 | + |
| 4 | +// MCP Client for The Graph |
| 5 | +class GraphMCPClient { |
| 6 | + private baseUrl: string; |
| 7 | + private headers: Record<string, string>; |
| 8 | + |
| 9 | + constructor(graphApiKey: string) { |
| 10 | + this.baseUrl = "https://subgraphs.mcp.thegraph.com"; |
| 11 | + this.headers = { |
| 12 | + Authorization: `Bearer ${graphApiKey}`, |
| 13 | + "Content-Type": "application/json", |
| 14 | + }; |
| 15 | + } |
| 16 | + |
| 17 | + async searchSubgraphs(keyword: string) { |
| 18 | + const response = await fetch(`${this.baseUrl}/search`, { |
| 19 | + method: "POST", |
| 20 | + headers: this.headers, |
| 21 | + body: JSON.stringify({ |
| 22 | + method: "search_subgraphs_by_keyword", |
| 23 | + params: { keyword }, |
| 24 | + }), |
| 25 | + }); |
| 26 | + |
| 27 | + if (!response.ok) { |
| 28 | + throw new Error(`MCP search failed: ${response.status}`); |
| 29 | + } |
| 30 | + |
| 31 | + return response.json(); |
| 32 | + } |
| 33 | + |
| 34 | + async getTopSubgraphsForContract(contractAddress: string, chain: string) { |
| 35 | + const response = await fetch(`${this.baseUrl}/contract-subgraphs`, { |
| 36 | + method: "POST", |
| 37 | + headers: this.headers, |
| 38 | + body: JSON.stringify({ |
| 39 | + method: "get_top_subgraph_deployments", |
| 40 | + params: { contract_address: contractAddress, chain }, |
| 41 | + }), |
| 42 | + }); |
| 43 | + |
| 44 | + if (!response.ok) { |
| 45 | + throw new Error(`MCP contract subgraph lookup failed: ${response.status}`); |
| 46 | + } |
| 47 | + |
| 48 | + return response.json(); |
| 49 | + } |
| 50 | + |
| 51 | + async getSubgraphSchema(subgraphId: string) { |
| 52 | + const response = await fetch(`${this.baseUrl}/schema`, { |
| 53 | + method: "POST", |
| 54 | + headers: this.headers, |
| 55 | + body: JSON.stringify({ |
| 56 | + method: "get_schema_by_subgraph_id", |
| 57 | + params: { subgraph_id: subgraphId }, |
| 58 | + }), |
| 59 | + }); |
| 60 | + |
| 61 | + if (!response.ok) { |
| 62 | + throw new Error(`MCP schema fetch failed: ${response.status}`); |
| 63 | + } |
| 64 | + |
| 65 | + return response.json(); |
| 66 | + } |
| 67 | + |
| 68 | + async executeQuery(subgraphId: string, query: string, variables?: Record<string, any>) { |
| 69 | + const response = await fetch(`${this.baseUrl}/query`, { |
| 70 | + method: "POST", |
| 71 | + headers: this.headers, |
| 72 | + body: JSON.stringify({ |
| 73 | + method: "execute_query_by_subgraph_id", |
| 74 | + params: { subgraph_id: subgraphId, query, variables }, |
| 75 | + }), |
| 76 | + }); |
| 77 | + |
| 78 | + if (!response.ok) { |
| 79 | + throw new Error(`MCP query execution failed: ${response.status}`); |
| 80 | + } |
| 81 | + |
| 82 | + return response.json(); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// Schema definitions |
| 87 | +const searchSubgraphsSchema = z.object({ |
| 88 | + keyword: z.string().describe("Keyword to search for in subgraph names and descriptions"), |
| 89 | +}); |
| 90 | + |
| 91 | +const getContractSubgraphsSchema = z.object({ |
| 92 | + contractAddress: z.string().describe("The contract address to find subgraphs for"), |
| 93 | + chain: z.string().describe("The blockchain network (e.g., 'mainnet', 'polygon', 'arbitrum-one')"), |
| 94 | +}); |
| 95 | + |
| 96 | +const getSchemaSchema = z.object({ |
| 97 | + subgraphId: z.string().describe("The subgraph ID to get the schema for"), |
| 98 | +}); |
| 99 | + |
| 100 | +const executeMCPQuerySchema = z.object({ |
| 101 | + subgraphId: z.string().describe("The subgraph ID to query"), |
| 102 | + query: z.string().describe("The GraphQL query string"), |
| 103 | + variables: z.record(z.any()).optional().describe("Optional variables for the GraphQL query"), |
| 104 | +}); |
| 105 | + |
| 106 | +export class GraphMCPProvider implements ActionProvider<WalletProvider> { |
| 107 | + name = "graph-mcp"; |
| 108 | + actionProviders = []; |
| 109 | + supportsNetwork = () => true; |
| 110 | + |
| 111 | + private mcpClient: GraphMCPClient; |
| 112 | + |
| 113 | + constructor() { |
| 114 | + const graphApiKey = process.env.GRAPH_API_KEY; |
| 115 | + if (!graphApiKey) { |
| 116 | + throw new Error("GRAPH_API_KEY not found in environment variables"); |
| 117 | + } |
| 118 | + this.mcpClient = new GraphMCPClient(graphApiKey); |
| 119 | + } |
| 120 | + |
| 121 | + getActions(walletProvider: WalletProvider) { |
| 122 | + return [ |
| 123 | + { |
| 124 | + name: "searchSubgraphs", |
| 125 | + description: "Search for subgraphs by keyword using The Graph's MCP. Returns relevant subgraphs with metadata.", |
| 126 | + schema: searchSubgraphsSchema, |
| 127 | + severity: "info" as const, |
| 128 | + invoke: async ({ keyword }: z.infer<typeof searchSubgraphsSchema>) => { |
| 129 | + try { |
| 130 | + const result = await this.mcpClient.searchSubgraphs(keyword); |
| 131 | + return JSON.stringify(result, null, 2); |
| 132 | + } catch (error) { |
| 133 | + return JSON.stringify({ |
| 134 | + error: error instanceof Error ? error.message : "Failed to search subgraphs", |
| 135 | + }); |
| 136 | + } |
| 137 | + }, |
| 138 | + }, |
| 139 | + { |
| 140 | + name: "getContractSubgraphs", |
| 141 | + description: "Find the top subgraphs that index a specific contract address on a given blockchain.", |
| 142 | + schema: getContractSubgraphsSchema, |
| 143 | + severity: "info" as const, |
| 144 | + invoke: async ({ contractAddress, chain }: z.infer<typeof getContractSubgraphsSchema>) => { |
| 145 | + try { |
| 146 | + const result = await this.mcpClient.getTopSubgraphsForContract(contractAddress, chain); |
| 147 | + return JSON.stringify(result, null, 2); |
| 148 | + } catch (error) { |
| 149 | + return JSON.stringify({ |
| 150 | + error: error instanceof Error ? error.message : "Failed to get contract subgraphs", |
| 151 | + }); |
| 152 | + } |
| 153 | + }, |
| 154 | + }, |
| 155 | + { |
| 156 | + name: "getSubgraphSchema", |
| 157 | + description: "Get the GraphQL schema for a specific subgraph, showing available entities and fields.", |
| 158 | + schema: getSchemaSchema, |
| 159 | + severity: "info" as const, |
| 160 | + invoke: async ({ subgraphId }: z.infer<typeof getSchemaSchema>) => { |
| 161 | + try { |
| 162 | + const result = await this.mcpClient.getSubgraphSchema(subgraphId); |
| 163 | + return JSON.stringify(result, null, 2); |
| 164 | + } catch (error) { |
| 165 | + return JSON.stringify({ |
| 166 | + error: error instanceof Error ? error.message : "Failed to get subgraph schema", |
| 167 | + }); |
| 168 | + } |
| 169 | + }, |
| 170 | + }, |
| 171 | + { |
| 172 | + name: "executeMCPQuery", |
| 173 | + description: "Execute a GraphQL query against a subgraph using The Graph's MCP protocol.", |
| 174 | + schema: executeMCPQuerySchema, |
| 175 | + severity: "info" as const, |
| 176 | + invoke: async ({ subgraphId, query, variables = {} }: z.infer<typeof executeMCPQuerySchema>) => { |
| 177 | + try { |
| 178 | + const result = await this.mcpClient.executeQuery(subgraphId, query, variables); |
| 179 | + return JSON.stringify(result, null, 2); |
| 180 | + } catch (error) { |
| 181 | + return JSON.stringify({ |
| 182 | + error: error instanceof Error ? error.message : "Failed to execute MCP query", |
| 183 | + }); |
| 184 | + } |
| 185 | + }, |
| 186 | + }, |
| 187 | + ]; |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +export const graphMCPProvider = () => new GraphMCPProvider(); |
0 commit comments