Skip to content

Dynamic Querying of Resources from Database Instead of Predefined Resources #51

@Jing-ze

Description

@Jing-ze

Problem

The mcp-go framework only returns predefined resources during the resources/list phase.

Expected

It would be highly beneficial if the resources/list phase could support dynamic queries to a database (or other data sources) and return dynamically generated resources, rather than relying solely on predefined configurations.

Example

The following code demonstrates how to dynamically query table names from a PostgreSQL database and return them as resources list

server.setRequestHandler(ListResourcesRequestSchema, async () => {
  const client = await pool.connect();
  try {
    const result = await client.query(
      "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'",
    );
    return {
      resources: result.rows.map((row) => ({
        uri: new URL(`${row.table_name}/${SCHEMA_PATH}`, resourceBaseUrl).href,
        mimeType: "application/json",
        name: `"${row.table_name}" database schema`,
      })),
    };
  } finally {
    client.release();
  }
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: sdkSDK improvements unrelated to MCP specification

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions