mcp-eth-test-subgraph-3
Version:
MCP server for ethereum_transactions_test subgraph - Track all Ethereum ERC20 transfers for testing
98 lines (97 loc) • 3.5 kB
JavaScript
export class GraphDatabaseService {
queryEndpoint;
subgraphId;
constructor(config) {
this.queryEndpoint = config.queryEndpoint;
this.subgraphId = config.subgraphId;
}
async executeCypherQuery(query, params = {}) {
try {
// Send query to dedicated query endpoint
const response = await fetch(`${this.queryEndpoint}/${this.subgraphId}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: query,
params: {
...params,
subgraph_id: this.subgraphId
}
}),
});
if (!response.ok) {
throw new Error(`Query request failed: ${response.status} ${response.statusText}`);
}
const result = await response.json();
if (!result.success) {
throw new Error(`Query execution failed: ${result.error || 'Unknown error'}`);
}
// Transform response to our QueryResult format
return {
records: result.records || result.data || [],
summary: {
query: {
text: query,
parameters: params,
},
queryType: 'READ',
counters: {
nodesCreated: 0,
nodesDeleted: 0,
relationshipsCreated: 0,
relationshipsDeleted: 0,
propertiesSet: 0,
},
resultConsumedAfter: 0,
resultAvailableAfter: 0,
},
};
}
catch (error) {
console.error('Error executing query:', error);
throw new Error(`Failed to execute query: ${error instanceof Error ? error.message : String(error)}`);
}
}
/**
* Test the connection to query endpoint
*/
async testConnection() {
try {
// Try a simple test query
const result = await this.executeCypherQuery('RETURN 1 as test', {});
return result.records.length > 0;
}
catch (error) {
console.error('Connection test failed:', error);
return false;
}
}
/**
* Get basic database statistics
*/
async getDatabaseStats() {
try {
// Use simple count queries
const nodeResult = await this.executeCypherQuery('MATCH (n) WHERE n.subgraph_id = $subgraph_id RETURN count(n) as nodeCount', {});
const relResult = await this.executeCypherQuery('MATCH ()-[r]->() WHERE r.subgraph_id = $subgraph_id RETURN count(r) as relCount', {});
return {
nodeCount: nodeResult.records[0]?.nodeCount || 0,
relCount: relResult.records[0]?.relCount || 0,
labelCount: 0,
relTypeCount: 0,
};
}
catch (error) {
console.warn('Could not fetch database stats:', error);
return { nodeCount: 0, relCount: 0, labelCount: 0, relTypeCount: 0 };
}
}
/**
* Close connection (no-op for HTTP-based service)
*/
async close() {
// No persistent connection to close
}
}