feat: Add audience_sql and segment_sql MCP tools#55
Merged
Conversation
This PR implements two new MCP tools for querying CDP segments: 1. `audience_sql`: Returns the base SQL statement for an audience (parent segment) 2. `segment_sql`: Returns the SQL statement for a segment with filtering conditions applied The implementation includes: - New CDP client methods: `getSegmentDetails()` and `getSegmentQuery()` - New TypeScript interfaces for segment rules and query requests/responses - Comprehensive unit tests for both the client methods and MCP tools - Updated documentation in CLAUDE.md These tools enable users to retrieve the underlying SQL queries that power CDP segments, making it easier to understand and work with segment data. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
xerial
commented
Jun 13, 2025
src/tools/cdp/segmentSql.ts
Outdated
|
|
||
| // First, we need to get the segment details to find the audience ID and rule | ||
| // We'll need to search through parent segments to find which one contains this segment | ||
| const parentSegments = await client.getParentSegments(); |
Member
Author
There was a problem hiding this comment.
This call has a higher overhead. Let's change the implementation plan:
- Change the MCP function as
segment_sql(audience_id, segment_id)to avoid this getParentSegments() call.
xerial
commented
Jun 13, 2025
| return response.data || []; | ||
| } | ||
|
|
||
| async getSegmentDetails(parentId: number, segmentId: number): Promise<SegmentDetails> { |
Member
Author
There was a problem hiding this comment.
Let's add a MCP function get_segment(audience_id, segment_id) for this method as well
…view feedback Based on PR review comments: 1. Changed segment_sql to accept both audience_id and segment_id parameters - This avoids the expensive getParentSegments() call - Improves performance by directly querying the segment 2. Added new get_segment tool for getSegmentDetails method - Provides direct access to segment details - Returns full segment information including rules 3. Updated all tests to match the new API signatures 4. Updated documentation in CLAUDE.md 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
Added documentation for the three new CDP tools: - get_segment: Get detailed segment information including rules - audience_sql: Get base SQL for an audience (parent segment) - segment_sql: Get SQL with filtering conditions for a segment Also added: - Response examples showing the SQL output format - CDP-specific prompt example for segment analysis - Updated initial setup prompt to mention CDP capabilities 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
Added comprehensive integration tests for: 1. New CDP client methods: - getSegmentDetails: Tests fetching detailed segment information - getSegmentQuery: Tests SQL generation for both audiences and segments 2. New MCP tools: - audience_sql: Tests base SQL generation for audiences - segment_sql: Tests SQL generation with filtering rules - get_segment: Tests fetching segment details through the tool 3. Error handling tests for all new tools with invalid IDs The tests run against the real development CDP API and handle cases where test data might not be available gracefully. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR implements GitHub issue #54 by adding two new MCP tools for querying CDP segments:
audience_sql: Returns the base SQL statement for an audience (parent segment)segment_sql: Returns the SQL statement for a segment with filtering conditions appliedChanges
getSegmentDetails(): Fetches detailed segment information including rulesgetSegmentQuery(): Generates SQL queries for audiences/segmentsTest plan
Closes #54
🤖 Generated with Claude Code