Skip to content

feat: Add audience_sql and segment_sql MCP tools#55

Merged
xerial merged 4 commits intomainfrom
feat/issue-54-segment-sql-tools
Jun 13, 2025
Merged

feat: Add audience_sql and segment_sql MCP tools#55
xerial merged 4 commits intomainfrom
feat/issue-54-segment-sql-tools

Conversation

@xerial
Copy link
Member

@xerial xerial commented Jun 13, 2025

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 applied

Changes

  • Added new CDP client methods:
    • getSegmentDetails(): Fetches detailed segment information including rules
    • getSegmentQuery(): Generates SQL queries for audiences/segments
  • Added new TypeScript interfaces for segment rules and query requests/responses
  • Created two new MCP tools with proper error handling
  • Added comprehensive unit tests for both client methods and MCP tools
  • Updated CLAUDE.md documentation with the new tools

Test plan

  • All existing tests pass
  • New unit tests added for CDP client methods
  • New unit tests added for MCP tools
  • TypeScript compilation successful
  • ESLint checks pass

Closes #54

🤖 Generated with Claude Code

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>

// 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();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

return response.data || [];
}

async getSegmentDetails(parentId: number, segmentId: number): Promise<SegmentDetails> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a MCP function get_segment(audience_id, segment_id) for this method as well

xerial and others added 3 commits June 12, 2025 22:52
…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>
@xerial xerial merged commit c8b35cf into main Jun 13, 2025
7 checks passed
@xerial xerial deleted the feat/issue-54-segment-sql-tools branch June 13, 2025 06:01
This was referenced Jun 13, 2025
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.

feat: Querying segments

1 participant