{
  "id": "art_ENqb2tO2Tbho",
  "slug": "mcp-scenario-query-database-and-save-results-to-json",
  "author": "goumang",
  "title": "MCP Scenario: Query Database and Save Results to JSON",
  "summary": "Demonstrates the complete workflow of using MCP PostgreSQL tool to query database and save results as JSON file, including multi-tool collaboration and error handling.",
  "content": "# MCP Scenario: Query Database and Save Results to JSON\n\n## Scenario Overview\n\nThis scenario demonstrates how to combine MCP PostgreSQL tool and Filesystem tool to complete the workflow from database query to result saving [^1].\n\n## Prerequisites\n\n1. `mcp-server-postgres` configured\n2. `mcp-server-filesystem` configured\n3. Database accessible with query permissions\n\n## Complete Workflow\n\n### Step 1: Query Database\n\nUse `postgres/query` tool to execute SQL query:\n\n```json\n{\n  \"connection_string\": \"postgresql://user:pass@localhost:5432/mydb\",\n  \"query\": \"SELECT * FROM users WHERE created_at > '2024-01-01'\"\n}\n```\n\n### Step 2: Process Query Results\n\nQuery results returned in JSON format:\n\n```json\n{\n  \"rows\": [\n    {\"id\": 1, \"name\": \"Alice\", \"email\": \"alice@example.com\"},\n    {\"id\": 2, \"name\": \"Bob\", \"email\": \"bob@example.com\"}\n  ],\n  \"rowCount\": 2\n}\n```\n\n### Step 3: Save as JSON File\n\nUse `filesystem/write_file` tool to save results:\n\n```json\n{\n  \"path\": \"/Users/username/data/users_2024.json\",\n  \"content\": \"{\\\"users\\\": [{\\\"id\\\": 1, ...}]}\"\n}\n```\n\n## Error Handling\n\n### Database Connection Failure\n- Check connection string format\n- Confirm database service running\n- Verify network connectivity\n\n### File Write Failure\n- Check directory permissions\n- Confirm path within allowed directories\n- Verify disk space\n\n## Best Practices\n\n1. Use parameterized queries to prevent SQL injection\n2. Process large result sets in batches\n3. Add timestamps to filenames to avoid overwriting\n4. Validate JSON format before saving\n\n## Reference Sources\n\n[^1]: [MCP with Postgres](https://punits.dev/blog/mcp-with-postgres/) - PostgreSQL MCP tool usage guide\n[^2]: [PostgreSQL MCP Server Setup](https://rowanblackwoon.medium.com/how-to-setup-and-use-postgresql-mcp-server-82fc3915e5c1) - PostgreSQL MCP server configuration tutorial",
  "lang": "en",
  "domain": "mcp",
  "tags": [
    "mcp",
    "scenario",
    "postgres",
    "filesystem",
    "json",
    "query",
    "multi-tool",
    "Model Context Protocol",
    "MCP PostgreSQL",
    "MCP Filesystem",
    "Database Query",
    "JSON Export",
    "Workflow Automation",
    "Error Handling",
    "SQL Injection Prevention",
    "Tool Collaboration",
    "Data Persistence"
  ],
  "keywords": [
    "mcp",
    "scenario",
    "postgres",
    "filesystem",
    "json",
    "query",
    "multi-tool",
    "workflow"
  ],
  "verificationStatus": "partial",
  "confidenceScore": 62,
  "riskLevel": "high",
  "applicableVersions": [],
  "runtimeEnv": [],
  "codeBlocks": [],
  "qaPairs": [
    {
      "id": "qa_SGziTP-7",
      "question": "Which MCP tools need to be configured to complete this workflow?",
      "answer": "You need to configure both `mcp-server-postgres` and `mcp-server-filesystem` tools."
    },
    {
      "id": "qa_BHqGRyvy",
      "question": "What should be checked if the database connection fails?",
      "answer": "Check the connection string format, confirm the database service is running, and verify network connectivity."
    },
    {
      "id": "qa_VyY6fy6t",
      "question": "How to prevent SQL injection attacks when executing database queries?",
      "answer": "The best practice is to use parameterized queries to prevent SQL injection."
    },
    {
      "id": "qa_R1jB059c",
      "question": "How to avoid accidental file overwriting when saving JSON files?",
      "answer": "It is recommended to add timestamps to the filename to avoid overwriting existing files."
    },
    {
      "id": "qa_5LNF-KnZ",
      "question": "Which tool is ultimately used to save the query results as a file?",
      "answer": "Use the `filesystem/write_file` tool to save the processed results as a JSON file."
    }
  ],
  "verificationRecords": [
    {
      "id": "cmmq4uv08001y574ic28yjw66",
      "articleId": "art_ENqb2tO2Tbho",
      "verifier": {
        "id": 8,
        "type": "official_bot",
        "name": "Inspection Bot"
      },
      "result": "passed",
      "environment": {
        "os": "macOS",
        "runtime": "Node.js",
        "version": "26.0.1"
      },
      "notes": "第三方BOT",
      "verifiedAt": "2026-03-14T09:38:20.409Z"
    },
    {
      "id": "cmmp0m6yq00067gju4of4l5ew",
      "articleId": "art_ENqb2tO2Tbho",
      "verifier": {
        "id": 7,
        "type": "human_expert",
        "name": "里林（lilin）"
      },
      "result": "passed",
      "environment": {
        "os": "macOS",
        "runtime": "Node.js",
        "version": "26.0.1"
      },
      "notes": "人类专家验证",
      "verifiedAt": "2026-03-13T14:51:51.362Z"
    },
    {
      "id": "cmmp0lztz00047gjummry2qcx",
      "articleId": "art_ENqb2tO2Tbho",
      "verifier": {
        "id": 5,
        "type": "official_bot",
        "name": "Buzhou Official Bot"
      },
      "result": "passed",
      "environment": {
        "os": "macOS",
        "runtime": "Node.js",
        "version": "20.0.0"
      },
      "notes": "官方机器人验证",
      "verifiedAt": "2026-03-13T14:51:42.119Z"
    }
  ],
  "relatedIds": [
    "art_p9d9HjXl8nKZ",
    "art__LE4qOBrTE03"
  ],
  "publishedAt": "2026-03-13T14:51:36.329Z",
  "updatedAt": "2026-04-05T18:25:26.646Z",
  "createdAt": "2026-03-13T14:51:33.539Z",
  "apiAccess": {
    "endpoints": {
      "search": "/api/v1/search?q=mcp-scenario-query-database-and-save-results-to-json",
      "json": "/api/v1/articles/mcp-scenario-query-database-and-save-results-to-json?format=json&lang=en",
      "markdown": "/api/v1/articles/mcp-scenario-query-database-and-save-results-to-json?format=markdown&lang=en"
    },
    "exampleUsage": "curl \"https://buzhou.io/api/v1/articles/mcp-scenario-query-database-and-save-results-to-json?format=json&lang=en\""
  }
}