-
Notifications
You must be signed in to change notification settings - Fork 109
Add structured content output to get_lineage tool #646
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jgiuffrida
wants to merge
3
commits into
main
Choose a base branch
from
jgiu/add-lineage-structured-content
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
.changes/unreleased/Enhancement or New Feature-20260313-125806.yaml
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| kind: Enhancement or New Feature | ||
| body: Add structured content output to get_lineage tool with LineageGraph model | ||
| time: 2026-03-13T12:58:06.544448-04:00 |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| import json | ||
| from unittest.mock import AsyncMock, Mock | ||
|
|
||
| import pytest | ||
| from mcp.types import CallToolResult, TextContent | ||
|
|
||
| from dbt_mcp.discovery.tools import ( | ||
| DiscoveryToolContext, | ||
| get_lineage as get_lineage_tool, | ||
| ) | ||
|
|
||
| # Access the underlying function from the ToolDefinition | ||
| get_lineage = get_lineage_tool.fn | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_discovery_tool_context(): | ||
| """Mock DiscoveryToolContext for testing.""" | ||
| context = Mock(spec=DiscoveryToolContext) | ||
| context.lineage_fetcher = AsyncMock() | ||
| return context | ||
|
|
||
|
|
||
| SAMPLE_NODES = [ | ||
| { | ||
| "uniqueId": "source.test.raw_customers", | ||
| "name": "raw_customers", | ||
| "resourceType": "Source", | ||
| "parentIds": [], | ||
| }, | ||
| { | ||
| "uniqueId": "model.test.customers", | ||
| "name": "customers", | ||
| "resourceType": "Model", | ||
| "parentIds": ["source.test.raw_customers"], | ||
| }, | ||
| { | ||
| "uniqueId": "model.test.customer_metrics", | ||
| "name": "customer_metrics", | ||
| "resourceType": "Model", | ||
| "parentIds": ["model.test.customers"], | ||
| }, | ||
| ] | ||
|
|
||
|
|
||
| async def test_get_lineage_returns_call_tool_result(mock_discovery_tool_context): | ||
| """Test that get_lineage returns a CallToolResult.""" | ||
| mock_discovery_tool_context.lineage_fetcher.fetch_lineage.return_value = ( | ||
| SAMPLE_NODES | ||
| ) | ||
|
|
||
| result = await get_lineage( | ||
| context=mock_discovery_tool_context, | ||
| unique_id="model.test.customers", | ||
| types=None, | ||
| depth=5, | ||
| ) | ||
|
|
||
| assert isinstance(result, CallToolResult) | ||
|
|
||
|
|
||
| async def test_get_lineage_text_content_contains_raw_nodes(mock_discovery_tool_context): | ||
| """Test that the text content contains the raw node data as JSON.""" | ||
| mock_discovery_tool_context.lineage_fetcher.fetch_lineage.return_value = ( | ||
| SAMPLE_NODES | ||
| ) | ||
|
|
||
| result = await get_lineage( | ||
| context=mock_discovery_tool_context, | ||
| unique_id="model.test.customers", | ||
| types=None, | ||
| depth=5, | ||
| ) | ||
|
|
||
| assert len(result.content) == 1 | ||
| assert isinstance(result.content[0], TextContent) | ||
| parsed = json.loads(result.content[0].text) | ||
| assert parsed == SAMPLE_NODES | ||
|
|
||
|
|
||
| async def test_get_lineage_structured_content_has_correct_graph( | ||
| mock_discovery_tool_context, | ||
| ): | ||
| """Test that structuredContent contains a well-formed LineageGraph.""" | ||
| mock_discovery_tool_context.lineage_fetcher.fetch_lineage.return_value = ( | ||
| SAMPLE_NODES | ||
| ) | ||
|
|
||
| result = await get_lineage( | ||
| context=mock_discovery_tool_context, | ||
| unique_id="model.test.customers", | ||
| types=None, | ||
| depth=5, | ||
| ) | ||
|
|
||
| sc = result.structuredContent | ||
| assert sc["type"] == "lineage_graph" | ||
| assert sc["root_id"] == "model.test.customers" | ||
| assert len(sc["nodes"]) == 3 | ||
| assert len(sc["edges"]) == 2 | ||
|
|
||
| node_ids = {n["unique_id"] for n in sc["nodes"]} | ||
| assert node_ids == { | ||
| "source.test.raw_customers", | ||
| "model.test.customers", | ||
| "model.test.customer_metrics", | ||
| } | ||
|
|
||
| edges = {(e["source"], e["target"]) for e in sc["edges"]} | ||
| assert edges == { | ||
| ("source.test.raw_customers", "model.test.customers"), | ||
| ("model.test.customers", "model.test.customer_metrics"), | ||
| } | ||
|
|
||
|
|
||
| async def test_get_lineage_filters_edges_to_known_nodes(mock_discovery_tool_context): | ||
| """Test that edges referencing nodes outside the graph are excluded.""" | ||
| nodes_with_external_parent = [ | ||
| { | ||
| "uniqueId": "model.test.customers", | ||
| "name": "customers", | ||
| "resourceType": "Model", | ||
| "parentIds": ["source.test.raw_customers", "model.other.unknown"], | ||
| }, | ||
| { | ||
| "uniqueId": "source.test.raw_customers", | ||
| "name": "raw_customers", | ||
| "resourceType": "Source", | ||
| "parentIds": [], | ||
| }, | ||
| ] | ||
| mock_discovery_tool_context.lineage_fetcher.fetch_lineage.return_value = ( | ||
| nodes_with_external_parent | ||
| ) | ||
|
|
||
| result = await get_lineage( | ||
| context=mock_discovery_tool_context, | ||
| unique_id="model.test.customers", | ||
| types=None, | ||
| depth=5, | ||
| ) | ||
|
|
||
| sc = result.structuredContent | ||
| # Only the edge from raw_customers should exist; model.other.unknown is not in the graph | ||
| assert len(sc["edges"]) == 1 | ||
| assert sc["edges"][0]["source"] == "source.test.raw_customers" | ||
| assert sc["edges"][0]["target"] == "model.test.customers" | ||
|
|
||
|
|
||
| async def test_get_lineage_empty_result(mock_discovery_tool_context): | ||
| """Test that an empty fetcher result produces an empty graph.""" | ||
| mock_discovery_tool_context.lineage_fetcher.fetch_lineage.return_value = [] | ||
|
|
||
| result = await get_lineage( | ||
| context=mock_discovery_tool_context, | ||
| unique_id="model.test.nonexistent", | ||
| types=None, | ||
| depth=5, | ||
| ) | ||
|
|
||
| sc = result.structuredContent | ||
| assert sc["nodes"] == [] | ||
| assert sc["edges"] == [] | ||
| assert sc["root_id"] == "model.test.nonexistent" | ||
|
|
||
|
|
||
| async def test_get_lineage_passes_parameters_to_fetcher(mock_discovery_tool_context): | ||
| """Test that parameters are correctly forwarded to the fetcher.""" | ||
| mock_discovery_tool_context.lineage_fetcher.fetch_lineage.return_value = [] | ||
|
|
||
| await get_lineage( | ||
| context=mock_discovery_tool_context, | ||
| unique_id="model.test.customers", | ||
| types=["Model", "Source"], | ||
| depth=3, | ||
| ) | ||
|
|
||
| mock_discovery_tool_context.lineage_fetcher.fetch_lineage.assert_called_once_with( | ||
| unique_id="model.test.customers", | ||
| types=["Model", "Source"], | ||
| depth=3, | ||
| ) | ||
|
|
||
|
|
||
| async def test_get_lineage_node_without_parent_ids(mock_discovery_tool_context): | ||
| """Test handling of nodes that lack a parentIds field.""" | ||
| nodes = [ | ||
| { | ||
| "uniqueId": "model.test.orphan", | ||
| "name": "orphan", | ||
| "resourceType": "Model", | ||
| # no parentIds key | ||
| }, | ||
| ] | ||
| mock_discovery_tool_context.lineage_fetcher.fetch_lineage.return_value = nodes | ||
|
|
||
| result = await get_lineage( | ||
| context=mock_discovery_tool_context, | ||
| unique_id="model.test.orphan", | ||
| types=None, | ||
| depth=5, | ||
| ) | ||
|
|
||
| sc = result.structuredContent | ||
| assert len(sc["nodes"]) == 1 | ||
| assert sc["edges"] == [] |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps
fetch_lineageshould return the pydantic model? Less dicts and more typed objects in general are better.