get_market_data
Retrieve Polymarket market data including outcomes, volumes, and conditions from the Main subgraph. Use order and pagination to customize results.
Instructions
Get Polymarket market/condition data including outcomes and volumes from the Main subgraph
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| first | No | Number of markets to return (1-100) | |
| orderBy | No | Field to order by | id |
| orderDirection | No | Sort direction | desc |
Implementation Reference
- src/index.ts:132-163 (registration)Tool 'get_market_data' is registered via server.registerTool on line 134. It accepts optional parameters (first, orderBy, orderDirection) and queries the Main subgraph for conditions data.
// Tool 4: get_market_data // --------------------------------------------------------------------------- server.registerTool( "get_market_data", { description: "Get Polymarket market/condition data including outcomes and volumes from the Main subgraph", inputSchema: { first: z.number().min(1).max(100).default(10).describe("Number of markets to return (1-100)"), orderBy: z.string().default("id").describe("Field to order by"), orderDirection: z.enum(["asc", "desc"]).default("desc").describe("Sort direction"), }, }, async ({ first, orderBy, orderDirection }) => { try { const query = `{ conditions(first: ${first}, orderBy: ${orderBy}, orderDirection: ${orderDirection}) { id oracle questionId outcomeSlotCount resolutionTimestamp payoutNumerators payoutDenominator } }`; const data = await querySubgraph(SUBGRAPHS.main.ipfsHash, query); return textResult(data); } catch (error) { return errorResult(error); } } ); - src/index.ts:144-162 (handler)The handler function (async callback) executes the tool logic: it constructs a GraphQL query against the Main subgraph's 'conditions' entity, calls querySubgraph, and returns the result.
async ({ first, orderBy, orderDirection }) => { try { const query = `{ conditions(first: ${first}, orderBy: ${orderBy}, orderDirection: ${orderDirection}) { id oracle questionId outcomeSlotCount resolutionTimestamp payoutNumerators payoutDenominator } }`; const data = await querySubgraph(SUBGRAPHS.main.ipfsHash, query); return textResult(data); } catch (error) { return errorResult(error); } } - src/index.ts:136-143 (schema)Input schema for the tool: 'first' (number, 1-100, default 10), 'orderBy' (string, default 'id'), 'orderDirection' (enum asc/desc, default 'desc') — defined via Zod.
{ description: "Get Polymarket market/condition data including outcomes and volumes from the Main subgraph", inputSchema: { first: z.number().min(1).max(100).default(10).describe("Number of markets to return (1-100)"), orderBy: z.string().default("id").describe("Field to order by"), orderDirection: z.enum(["asc", "desc"]).default("desc").describe("Sort direction"), }, }, - src/graphClient.ts:12-59 (helper)The querySubgraph function used by the handler to execute the GraphQL query against The Graph's decentralized network.
export async function querySubgraph( ipfsHash: string, query: string, variables?: Record<string, unknown> ): Promise<unknown> { const apiKey = process.env.GRAPH_API_KEY; if (!apiKey) { throw new GraphClientError( "GRAPH_API_KEY environment variable is required. " + "Get one at https://thegraph.com/studio/apikeys/" ); } const url = `https://gateway.thegraph.com/api/${apiKey}/deployments/id/${ipfsHash}`; const body: Record<string, unknown> = { query }; if (variables && Object.keys(variables).length > 0) { body.variables = variables; } const response = await fetch(url, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), }); if (!response.ok) { throw new GraphClientError( `Graph API returned HTTP ${response.status}: ${response.statusText}`, response.status ); } const json = (await response.json()) as { data?: unknown; errors?: unknown[]; }; if (json.errors && json.errors.length > 0) { throw new GraphClientError( `GraphQL errors: ${JSON.stringify(json.errors)}`, undefined, json.errors ); } return json.data; }