Context Graph MCP best practices

Updated at:

This topic provides two best practices for using DataWorks Context Graph through MCP: verify report metric definitions and data sources with a read-only agent, and build a context graph from specified tables and retrieve the results with a management agent. Each case lists the MCP APIs to select, the procedure, and example dialogs.

Case overview

Case

Scenario

What you do

Case 1: Verify report metric definitions with a read-only agent

Metric values in a business dashboard and a monthly business report do not match. Verify the statistical definitions and data sources.

Configure a read-only MCP service, retrieve metric definitions, and read knowledge point details.

Case 2: Build a context graph with a management agent

After a new data domain goes live, provide downstream teams with table purposes, key fields, and table relationships.

Create a knowledge base, select tables, run a one-time context graph build, and retrieve the results.

Case 1: Verify report metric definitions with a read-only agent

Scenario and prerequisites

When metric values in a business dashboard and a monthly business report do not match, use a read-only agent to query published metric definitions and verify the statistical period, order scope, refund rules, and data sources. This case uses gross merchandise volume (GMV) as an example. It does not run SQL or read order details.

A searchable business metrics knowledge base is created, and you have read permissions on it. The knowledge base should contain the following content:

Knowledge

Recommended content

Metric definitions confirmed by business owners

Applicable reports, effective dates, statistical granularity, inclusion and exclusion criteria, and exception rules.

Recommended data asset descriptions

Source tables, fields, purposes, and applicable scope. Include Data Map entries when needed.

The example refers to the order creation and order payment scenarios in the DataWorks retail and e-commerce industry model. Actual report definitions are subject to the metric definitions published by your enterprise.

Preparation

This case uses the Custom edition of Alibaba Cloud OpenAPI MCP Server. If you already have a read-only service with the following APIs, skip the preparation and go directly to Verify report metric definitions.

1. Create a read-only MCP service

  1. Log on to the Alibaba Cloud OpenAPI MCP console and choose Custom > Create.

  2. Enter a service name, such as dw-kb-search. For OAuth, select Alibaba Cloud official OAuth.

  3. In Cloud Products and API List, select DataWorks and version 2024-05-18. In the API search box, enter each full API name in the following table and select the matching API.

    API search keyword

    Purpose

    ListKnowledgeBases

    Lists knowledge bases to locate the target by name. Optional if you already know the knowledge base ID.

    RetrieveKnowledgePoints

    Retrieves knowledge points. Do not select ListKnowledgePoints, which is for paginated browsing.

    GetKnowledgePoint

    Reads knowledge point details for verification and citation.

  4. Click Create. After creation, copy the Streamable HTTP Endpoint from the service details. Use the full endpoint provided by the console. Do not replace the region in the endpoint domain with the region of the knowledge base.

For other authentication or network options, see Access Context Graph via MCP and OpenAPI MCP Server user guide.

2. Add the connection in Qwen Work and authorize

Open the Qwen Work desktop client and go to Extensions > Connectors. Click + Add > Manual Configuration. Fill in the following table and save.

Parameter

Description

Server name

Custom, such as DataWorks Context Graph Search.

Server type

Select Streamable HTTP.

Server endpoint

Enter the Streamable HTTP Endpoint obtained from the Alibaba Cloud console in the previous step.

Custom request headers

Leave empty. This case uses OAuth authorization.

Enable the connection in the connector list. On the first connection or call, complete OAuth authorization in the browser as prompted. Use an Alibaba Cloud identity with read permissions on the target knowledge base. If the client still prompts for authentication, restart authorization from the authentication entry of the connection.

For the Qwen Work entry point, see Qwen Work configuration guide.

3. Confirm the connection works

If you added ListKnowledgeBases, start a conversation in Qwen Work and send the following query:

Use the DataWorks Context Graph Search connection to list the knowledge bases I can access in China (Hangzhou). Only query. Do not create or modify any resources.

Replace the region and connection name with your actual values. If the connection has a default region, you can omit the region. If you did not add the list API, use the example prompts below to query a specific knowledge base directly.

If the connection or tool list behaves unexpectedly, troubleshoot by following the FAQ in Access Context Graph via MCP.

Verify report metric definitions

Step 1: Query the report metric definitions

Send the following prompt in the Qwen Work conversation:

The GMV in the monthly business report does not match the business dashboard. Search the Business Metrics knowledge base for the current report definition: Is it based on order time or payment time? How are canceled orders and refunds handled? Which table and field should the report use? Cite the sources and separately list any rules that cannot be confirmed.

If multiple knowledge bases share the same name, select the target by region. If multiple metrics share the same name, confirm the definition by applicable report, effective date, and statistical granularity.

Step 2: Read knowledge point details

The agent uses Context Graph retrieval to find metric definitions and source tables in the specified knowledge base, then reads the relevant knowledge point details to verify rules and sources.

If the two reports belong to different knowledge bases, specify both in one prompt. If a rule is not recorded in the knowledge base, list it as pending confirmation in the answer.

Step 3: Review the verification result

The following answer is an example. It assumes the knowledge base records the statistical definition and source tables for the monthly business report, but not the cross-month refund rule.

Confirmed: The monthly report attributes orders to months by payment date and includes only successfully paid orders. Orders canceled before payment are excluded. Sources: Statistical period and Order scope in Business Monthly Report Metric Definitions.
Data retrieval: The current definition points to the order analysis table but does not specify the physical field used by the report. Before generating SQL, verify the report configuration or ask the metric owner.
Pending confirmation: The current description does not specify which month a cross-month refund deducts from, so it cannot explain the difference between the two reports.

Case 2: Build a context graph with a management agent

Scenario and prerequisites

After a new data domain goes live, downstream teams need to understand table purposes, key date fields, and table relationships. This case creates a knowledge base, selects three managed tables, and builds a context graph. After the build, retrieve the relevant knowledge points in the knowledge base.

  • Data Agent is activated in the target region.

  • A workspace and an available resource group are prepared for running tasks. The workspace provides the execution resources. The build scope is determined by the selected tables.

  • Metadata collection is complete for the tables to build in Data Map.

For reproducibility, this example uses web_sales, web_returns, and date_dim from the official TPC-DS model. Replace them with your own fact tables and shared dimension tables. This example does not upload local files or provide business details of the source tables in the conversation.

Preparation

1. Create a management MCP service

Follow Preparation in Case 1 to create a management service, such as dw-kb-manage.

Under DataWorks version 2024-05-18, search for each API name in the following table:

API search keyword

Purpose

ListKnowledgeBases

Lists knowledge bases to find one to reuse. Optional if you already know the knowledge base ID.

CreateKnowledgeBase

Creates a knowledge base. Optional when reusing an existing one.

CreateKnowledgeBaseBuildTask

Creates a manual build task.

GetKnowledgeBaseBuildTask

Reads the build task configuration to verify the table scope, execution resources, and trigger method.

CreateKnowledgeBaseBuildTaskInstance

Creates a build task instance to start a one-time build.

GetKnowledgeBaseBuildTaskInstance

Queries the running status of a specific instance.

RetrieveKnowledgePoints

Retrieves knowledge in the new knowledge base after the build.

GetKnowledgePoint

Reads the details of retrieved knowledge points.

Also select ListKnowledgeBaseBuildTasks and ListKnowledgeBaseBuildTaskInstances to check whether a task or instance already exists after a creation timeout. To let the agent find execution resources on its own, optionally add ListProjects, ListTables, ListResourceGroups, and ListResourceGroupAssociateProjects. Alternatively, confirm the resources in the DataWorks console and provide them to the agent.

2. Add the connection in Qwen Work and authorize

Follow 2. Add the connection in Qwen Work and authorize in Case 1 to add the Streamable HTTP endpoint of the management service to Qwen Work, and complete OAuth authorization with an identity that has management permissions on the target knowledge base. Keep the original read-only service and do not add write APIs to it.

Build a context graph and retrieve results

Step 1: Confirm the data scope and create a build task

Send the following prompt in the Qwen Work conversation to confirm the data scope first:

I want to hand over the new trading domain to the operations team. Select only web_sales, web_returns, and date_dim in the test workspace. List the full names of the three tables, the execution workspace, and the resource group. Do not create anything before I confirm.

After confirmation, continue:

Create a manual build task named asset-handover-build in a separate knowledge base named Trading Domain Pilot. Build only the three tables just confirmed. Do not enable scheduled builds and do not modify existing business knowledge bases. Show me the scope after creation, and I will decide whether to run it.

After the agent creates the task, query the task configuration and verify the selected tables, execution workspace, resource group, and trigger method. This example selects data assets by table directly, so no separate knowledge source registration is required.

Step 2: Run the build task

Send the following prompt to start a one-time build:

Run once with the confirmed configuration. Do not enable scheduled builds. Tell me the instance ID and the result.

The agent starts a build and queries the running status by instance ID. After the build succeeds, proceed to retrieval. A created task means the configuration is saved. An accepted run request means the instance is created. Neither means the build is complete. If the build fails, keep the instance ID, error message, and request ID for troubleshooting.

Note

If creation or startup times out, ask the agent to check whether the task or instance already exists before retrying, to avoid duplicate creation.

Step 3: Retrieve the build results

Send the following prompt to retrieve knowledge in the new knowledge base:

Search only the Trading Domain Pilot knowledge base: Which tables are used for web sales and returns? Which date dimension table supports calendar-month analysis? If the knowledge base records the join condition between sales and returns, cite the source.

Retrieve and open the details of the sales, returns, and date dimension tables in the new knowledge base. Describe each table's purpose based on the details, and distinguish sales dates from return dates. If neither the knowledge point details nor the relationship paths provide a join condition, do not assume how the tables are joined. If a table is not retrieved, verify the input scope of the build instance and the table metadata in Data Map.

The first delivery covers only the confirmed core tables. To expand the scope, enable scheduled builds, or share the knowledge base, get approval from the owner and then configure the corresponding knowledge base OpenAPI tools.