Skip to main content
Glama
ctkadvisors

GraphQL MCP Server

by ctkadvisors

country

Query detailed country information using a specific country code via the GraphQL MCP Server. Retrieve essential data for analysis or integration with Claude AI workflows.

Instructions

GraphQL country query

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
codeYescode parameter (ID)

Implementation Reference

  • The tools/list handler dynamically generates and registers all query tools from the GraphQL schema fetched from the countries API, including the 'country' tool.
    if (method === "tools/list") {
      // Get schema (will use cache if available)
      const schema = await fetchSchema();
      const queryTools = getToolsFromSchema(schema);
      
      // Only include mutations if they are enabled
      const mutationTools = ENABLE_MUTATIONS ? getToolsFromMutationType(schema) : [];
      const allTools = [...queryTools, ...mutationTools];
    
      log(
        "info",
        `Returning ${allTools.length} tools (${queryTools.length} queries, ${mutationTools.length} mutations${!ENABLE_MUTATIONS ? ' - mutations disabled' : ''})`
      );
    
      const response: JSONRPCResponse = {
        jsonrpc: "2.0",
        id,
        result: {
          tools: allTools,
        },
      };
      console.log(JSON.stringify(response));
      return;
  • The core handler function for executing query-based tools like 'country'. It fetches the GraphQL schema, resolves the 'country' field, processes input arguments, dynamically builds the GraphQL query with appropriate selection set, and executes it against the countries API.
    async function executeGraphQLTool(
      name: string,
      args: Record<string, any> | undefined
    ): Promise<any> {
      try {
        // If this name is in our mapping, use the original field name
        const actualFieldName = toolNameMappings[name] || name;
    
        // Check if the tool is in the whitelist (if whitelist is enabled)
        if (WHITELISTED_QUERIES && !WHITELISTED_QUERIES.includes(actualFieldName)) {
          throw new Error(`Tool '${actualFieldName}' is not in the whitelist`);
        }
    
        // Get the schema
        const schema = await fetchSchema();
        if (!schema) {
          throw new Error("Schema not available");
        }
    
        // Get the query type
        const queryType = schema.getQueryType();
        if (!queryType) {
          throw new Error("Schema has no query type");
        }
    
        // Get the field for this tool using the resolved field name
        const fields = queryType.getFields();
        const field = fields[actualFieldName];
    
        if (!field) {
          throw new Error(`Unknown field: ${actualFieldName}`);
        }
    
        try {
          // Process input arguments
          const processedArgs = processArguments(args, field.args, schema);
    
          // Get return type and analyze it
          const returnType = getNamedType(field.type);
    
          // First determine which arguments are actually being used
          const usedArgNames: string[] = [];
          field.args.forEach((arg) => {
            if (processedArgs && processedArgs[arg.name] !== undefined) {
              usedArgNames.push(arg.name);
            }
          });
    
          // Build variables definition - only for arguments that are actually used
          const varDefs = field.args
            .filter((arg) => usedArgNames.includes(arg.name))
            .map((arg) => {
              // Need to preserve non-null and list wrappers in variable definitions
              const typeStr = arg.type.toString();
              return `$${arg.name}: ${typeStr}`;
            })
            .filter(Boolean)
            .join(", ");
    
          // Build field arguments - only for arguments that are actually used
          const fieldArgs = usedArgNames
            .map((argName) => {
              return `${argName}: $${argName}`;
            })
            .join(", ");
    
          // Build selection set based on return type
          let selectionSet = "";
    
          if (isObjectType(returnType)) {
            // For objects or lists of objects, analyze fields
            const fields = analyzeFields(returnType, schema);
            selectionSet = buildSelectionSet(fields);
          } else if (isListType(returnType)) {
            // For lists, unwrap and analyze the inner type
            const innerType = getNamedType(returnType.ofType);
            if (isObjectType(innerType)) {
              const fields = analyzeFields(innerType as GraphQLObjectType, schema);
              selectionSet = buildSelectionSet(fields);
            }
          }
    
          // Build the final query
          // Only include variable definitions if fieldArgs is used
          const shouldIncludeVarDefs = fieldArgs && fieldArgs.length > 0;
          const query = `
            query ${name}Query${shouldIncludeVarDefs ? `(${varDefs})` : ""} {
              ${name}${fieldArgs && fieldArgs.length > 0 ? `(${fieldArgs})` : ""} ${
            selectionSet ? `{\n${selectionSet}  }` : ""
          }
            }
          `;
    
          log("debug", `Generated query for ${name}:`, {
            query,
            variables: processedArgs,
          });
    
          // Execute the query
          return await executeQuery({ query, variables: processedArgs });
        } catch (error) {
          throw new Error(
            `Error executing query for ${name}: ${
              error instanceof Error ? error.message : String(error)
            }`
          );
        }
      } catch (error) {
        log(
          "error",
          `Error executing tool ${name}: ${
            error instanceof Error ? error.message : String(error)
          }`
        );
        throw error;
      }
    }
  • Dynamically generates the input schema (JSON Schema) for each GraphQL query field tool, including 'country', by introspecting the field's arguments and mapping GraphQL types to JSON Schema types.
    function getToolsFromSchema(schema: GraphQLSchema | null): MCPTool[] {
      if (!schema) {
        return []; // Return empty tools list if no schema available
      }
    
      const tools: MCPTool[] = [];
    
      // Get the query type from the schema
      const queryType = schema.getQueryType();
      if (!queryType) {
        log("warning", "Schema has no query type");
        return tools;
      }
    
      // Get all available query fields
      const fields = queryType.getFields();
    
      // Process each query field as a potential tool
      for (const [fieldName, field] of Object.entries(fields)) {
        // Skip fields that aren't in the whitelist (if a whitelist is provided)
        if (WHITELISTED_QUERIES && !WHITELISTED_QUERIES.includes(fieldName)) {
          if (DEBUG) {
            log("debug", `Skipping field ${fieldName} - not in whitelist`);
          }
          continue;
        }
        try {
          // Analyze arguments for the field
          const properties: Record<string, any> = {};
          const required: string[] = [];
    
          // Process arguments
          field.args.forEach((arg) => {
            // Get the actual GraphQL type (unwrap non-null and list types)
            let argType = arg.type;
            let isNonNull = false;
    
            if (isNonNullType(argType)) {
              isNonNull = true;
              argType = argType.ofType;
            }
    
            let isList = false;
            if (isListType(argType)) {
              isList = true;
              argType = argType.ofType;
              // Unwrap non-null inside list if needed
              if (isNonNullType(argType)) {
                argType = argType.ofType;
              }
            }
    
            const baseType = getNamedType(argType);
    
            // Create property definition
            if (isScalarType(baseType)) {
              // For scalar types like Int, String, etc.
              properties[arg.name] = {
                type: getJsonSchemaType(baseType.name),
                description:
                  arg.description || `${arg.name} parameter (${baseType.name})`,
              };
            } else if (isEnumType(baseType)) {
              // For enum types, specify allowed values
              const enumValues = baseType.getValues().map((val) => val.name);
              properties[arg.name] = {
                type: "string",
                enum: enumValues,
                description:
                  arg.description || `${arg.name} parameter (${baseType.name})`,
              };
            } else if (isInputObjectType(baseType)) {
              // For input object types (complex types), describe it as object
              properties[arg.name] = {
                type: "object",
                description: `${arg.name} - Input type: ${baseType.name}`,
              };
    
              // If we wanted to go deeper, we could recursively define the object structure:
              // properties[arg.name].properties = getInputObjectProperties(baseType);
            } else {
              // Default catch-all
              properties[arg.name] = {
                type: "string",
                description: arg.description || `${arg.name} parameter`,
              };
            }
    
            // If arg is non-null, add to required list
            if (isNonNull) {
              required.push(arg.name);
            }
          });
    
          // Create a tool for this field - Truncate name to 64 characters if needed
          const truncatedName =
            fieldName.length > 64 ? fieldName.substring(0, 64) : fieldName;
    
          // Store mapping from truncated to original name if truncation occurred
          if (truncatedName !== fieldName) {
            toolNameMappings[truncatedName] = fieldName;
          }
    
          const tool: MCPTool = {
            name: truncatedName,
            description: field.description || `GraphQL ${fieldName} query`,
            inputSchema: {
              type: "object",
              properties: properties,
              required: required,
            },
          };
    
          tools.push(tool);
        } catch (error) {
          log(
            "error",
            `Error processing field ${fieldName}: ${
              error instanceof Error ? error.message : String(error)
            }`
          );
        }
      }
    
      return tools;
    }
  • Fetches and caches the GraphQL schema from https://countries.trevorblades.com/graphql via introspection, which defines the 'country' query field used to generate the tool.
    async function fetchSchema(): Promise<GraphQLSchema | null> {
      // Prevent concurrent schema fetches
      if (schemaFetchInProgress) {
        log("info", "Schema fetch already in progress, waiting for it to complete");
        while (schemaFetchInProgress) {
          await new Promise((resolve) => setTimeout(resolve, 50));
        }
        return schemaCache;
      }
    
      // Check if we have a valid cached schema
      const now = Date.now();
      if (schemaCache && schemaCacheExpiry && now < schemaCacheExpiry) {
        log("info", "Using cached schema");
        return schemaCache;
      }
    
      try {
        schemaFetchInProgress = true;
        log("info", `Fetching GraphQL schema from ${GRAPHQL_API_ENDPOINT}`);
    
        // Build headers
        const headers: Record<string, string> = {};
        if (GRAPHQL_API_KEY) {
          headers.Authorization = `Bearer ${GRAPHQL_API_KEY}`;
        }
    
        // Create client and execute introspection query
        const client = new GraphQLClient(GRAPHQL_API_ENDPOINT, { headers });
        const startTime = Date.now();
        const data = await client.request<IntrospectionQuery>(
          getIntrospectionQuery()
        );
        const schema = buildClientSchema(data);
        const duration = Date.now() - startTime;
    
        log("info", `Schema fetched successfully in ${duration}ms`);
    
        // Cache the schema and reset the name mappings
        schemaCache = schema;
        schemaCacheExpiry = now + CACHE_TTL;
        toolNameMappings = {}; // Reset mappings as schema might have changed
    
        return schema;
      } catch (error) {
        log(
          "error",
          `Error fetching schema: ${
            error instanceof Error ? error.message : String(error)
          }`
        );
        return null;
      } finally {
        schemaFetchInProgress = false;
      }
    }
  • Configuration defining the countries GraphQL API endpoint, which provides the 'country' query field that becomes the MCP 'country' tool.
    const GRAPHQL_API_ENDPOINT: string =
      process.env.GRAPHQL_API_ENDPOINT ||
      "https://countries.trevorblades.com/graphql";
    const GRAPHQL_API_KEY: string = process.env.GRAPHQL_API_KEY || "";
    const DEBUG: boolean = process.env.DEBUG === "true";
    const CACHE_TTL: number = 3600000; // 1 hour in milliseconds
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries full burden. It mentions 'GraphQL' and 'query', implying a read-only operation, but doesn't disclose behavioral traits like error handling, rate limits, authentication needs, or what happens if the code is invalid. Minimal context beyond the basic operation.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is very concise with two words, but it's under-specified rather than efficiently informative. It's front-loaded but lacks necessary detail, making it borderline between conciseness and insufficiency.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given no annotations, no output schema, and a single parameter with full schema coverage, the description is incomplete. It doesn't explain what the query returns, error conditions, or how it differs from sibling tools, leaving significant gaps for an AI agent.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100% with one parameter 'code' documented as 'code parameter (ID)'. The description adds no additional meaning beyond what the schema provides, such as format examples or constraints. Baseline 3 is appropriate since the schema does the heavy lifting.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose2/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description 'GraphQL country query' states the technology and resource but lacks a specific verb and doesn't distinguish from siblings like 'countries' or 'language'. It's vague about what the query actually does (fetch by code vs general query).

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

No guidance on when to use this tool versus siblings like 'countries' (plural) or 'language'. The description implies it's for querying countries via GraphQL, but doesn't specify use cases, prerequisites, or alternatives.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/ctkadvisors/graphql-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server