Skip to content

feat: add --result-format CLI arg for compact query results (csv)#171

Open
Finndersen wants to merge 1 commit intoSnowflake-Labs:mainfrom
Finndersen:feat/result-format-cli-arg
Open

feat: add --result-format CLI arg for compact query results (csv)#171
Finndersen wants to merge 1 commit intoSnowflake-Labs:mainfrom
Finndersen:feat/result-format-cli-arg

Conversation

@Finndersen
Copy link
Copy Markdown

Summary

  • Adds a --result-format CLI argument (choices: json, csv) that controls how SQL query results are returned to the LLM
  • CSV format writes column names once in a header row instead of repeating them for every row, reducing token usage for large result sets
  • Configurable via environment variable SNOWFLAKE_MCP_RESULT_FORMAT as well as the CLI flag

Motivation

Query results are returned as list[dict], where column names repeat for every row. For large result sets this can be wasteful with LLM tokens. CSV format can significantly reduce result size with no new dependencies (uses Python stdlib csv module).

Changes

  • server.py: Add --result-format arg to parse_arguments(); store result_format on SnowflakeService; pass through from create_lifespan
  • utils.py: Add results_to_csv() utility; apply format at execute_query() and SnowflakeResponse.fetch_results(); update AnalystResponse.results type to str | dict | list to accommodate CSV-formatted embedded results
  • query_manager/tools.py: Apply format at run_query()
  • README.md: Document the new option with usage example and caveats
  • tests/test_result_format.py: Tests for results_to_csv(), SnowflakeService.result_format, and CLI arg parsing

Caveats

  • CSV format applies to SQL query results only (not Cortex Search/Agent responses, which return free-text)
  • CSV loses type information — numbers, booleans, and nulls are all represented as strings
  • Default remains json so existing behaviour is unchanged

Test plan

  • Run pytest mcp_server_snowflake/tests/ — all tests pass
  • Start server with --result-format csv and run a SQL query — verify CSV string with header row is returned
  • Start server with default (no flag) — verify JSON array output is unchanged

Adds a --result-format option (json/csv) to reduce token usage for large
result sets. CSV format writes column names once in a header row rather
than repeating them for every row.

- Add --result-format arg to parse_arguments() with SNOWFLAKE_MCP_RESULT_FORMAT env var fallback
- Store result_format on SnowflakeService and pass through from create_lifespan
- Add results_to_csv() utility in utils.py; inline the format conditional at each of the three query return points (execute_query, fetch_results, run_query)
- Update AnalystResponse.results type to accept str for CSV-formatted embedded results
- Add tests for results_to_csv(), SnowflakeService.result_format, and CLI arg parsing
- Document --result-format in README
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant