Class PostgresMcpClient

java.lang.Object
com.tnsai.mcp.database.PostgresMcpClient

public class PostgresMcpClient extends Object
Client for PostgreSQL MCP server.

Provides convenience methods for common PostgreSQL operations by wrapping MCP tool calls to a PostgreSQL MCP server. All operations are performed through the underlying McpClient.

Example usage:


 McpClient mcpClient = McpClient.create(transport);
 mcpClient.connect();
 mcpClient.initialize();

 PostgresMcpClient postgres = new PostgresMcpClient(mcpClient);
 JsonNode tables = postgres.listTables();
 JsonNode result = postgres.executeQuery("SELECT * FROM users LIMIT 10");
 
See Also:
  • Constructor Details

    • PostgresMcpClient

      public PostgresMcpClient(McpClient mcpClient)
      Creates a new PostgresMcpClient wrapping the given MCP client.
      Parameters:
      mcpClient - the MCP client connected to a PostgreSQL MCP server
      Throws:
      IllegalArgumentException - if mcpClient is null
  • Method Details

    • executeQuery

      public com.fasterxml.jackson.databind.JsonNode executeQuery(String sql) throws IOException
      Executes a SQL query.
      Parameters:
      sql - the SQL query to execute
      Returns:
      the query results as JSON
      Throws:
      IOException - if the MCP call fails
    • listTables

      public com.fasterxml.jackson.databind.JsonNode listTables() throws IOException
      Lists all tables in the database.
      Returns:
      the list of tables as JSON
      Throws:
      IOException - if the MCP call fails
    • describeTable

      public com.fasterxml.jackson.databind.JsonNode describeTable(String table) throws IOException
      Describes the schema of a table.
      Parameters:
      table - the table name
      Returns:
      the table description as JSON
      Throws:
      IOException - if the MCP call fails
    • getMcpClient

      public McpClient getMcpClient()
      Returns the underlying MCP client.
      Returns:
      the MCP client
    • isAvailable

      public boolean isAvailable()
      Checks if the PostgreSQL MCP server is available.
      Returns:
      true if the MCP client is connected and initialized