@@ -21,7 +21,6 @@ import { Source } from '@/models/source';
2121import { callTool , createTestClient , getFirstText } from './mcpTestUtils' ;
2222
2323const mockFetch = jest . mocked ( global . fetch ) ;
24- const { realFetch } = globalThis as unknown as { realFetch : typeof fetch } ;
2524
2625const TABLE = 'mcp_promql_sources' ;
2726
@@ -80,6 +79,13 @@ describe('MCP PromQL sources', () => {
8079 startSec : nowSec - 600 ,
8180 endSec : nowSec - 60 ,
8281 } ,
82+ // Two days old: outside the old 24-hour catalog window.
83+ {
84+ metricName : 'stale_metric' ,
85+ tags : { job : 'batch' , region : 'eu' } ,
86+ startSec : nowSec - 3 * 86400 ,
87+ endSec : nowSec - 2 * 86400 ,
88+ } ,
8389 ] ,
8490 withSamples : true ,
8591 } ) ;
@@ -167,7 +173,7 @@ describe('MCP PromQL sources', () => {
167173 expect ( text ) . toContain ( 'up' ) ;
168174 } ) ;
169175
170- it ( 'describes a TimeSeries table by its label names and metric names ' , async ( ) => {
176+ it ( 'describes a TimeSeries table by its metric names, with no time window ' , async ( ) => {
171177 const result = await callTool ( client , 'clickstack_describe_source' , {
172178 sourceId : promqlSource . _id . toString ( ) ,
173179 } ) ;
@@ -180,14 +186,80 @@ describe('MCP PromQL sources', () => {
180186 backend : 'clickhouse' ,
181187 table : `${ DEFAULT_DATABASE } .${ TABLE } ` ,
182188 } ) ;
183- expect ( output . labelNames ) . toEqual ( [ '__name__' , 'instance' , 'job' ] ) ;
184- expect ( output . metricNames ) . toEqual ( [ 'http_requests_total' , 'up' ] ) ;
189+ expect ( output . labelNames ) . toBeUndefined ( ) ;
190+ expect ( output . metricNames ) . toEqual ( [
191+ 'http_requests_total' ,
192+ 'stale_metric' ,
193+ 'up' ,
194+ ] ) ;
195+ expect ( output . note ) . toContain ( 'metricName' ) ;
185196 expect ( output . columns ) . toBeUndefined ( ) ;
186197 expect ( output . nextSteps . sql ) . toContain (
187198 `timeSeriesData(${ DEFAULT_DATABASE } .${ TABLE } )` ,
188199 ) ;
189200 } ) ;
190201
202+ it ( "lists one metric's label names with metricName" , async ( ) => {
203+ const result = await callTool ( client , 'clickstack_describe_source' , {
204+ sourceId : promqlSource . _id . toString ( ) ,
205+ metricName : 'stale_metric' ,
206+ } ) ;
207+
208+ const output = JSON . parse ( getFirstText ( result ) ) ;
209+ expect ( output . labelNames ) . toEqual ( [ '__name__' , 'job' , 'region' ] ) ;
210+ expect ( output . metricNames ) . toContain ( 'stale_metric' ) ;
211+ } ) ;
212+
213+ it ( 'says so when no series has the metricName' , async ( ) => {
214+ const result = await callTool ( client , 'clickstack_describe_source' , {
215+ sourceId : promqlSource . _id . toString ( ) ,
216+ metricName : 'no_such_metric' ,
217+ } ) ;
218+
219+ const output = JSON . parse ( getFirstText ( result ) ) ;
220+ expect ( output . labelNames ) . toEqual ( [ ] ) ;
221+ expect ( output . note ) . toContain (
222+ 'No series has the metric name "no_such_metric".' ,
223+ ) ;
224+ } ) ;
225+
226+ it ( 'says a metric had no recent samples when an endpoint has no labels for it' , async ( ) => {
227+ const source = await createPrometheusSource ( ) ;
228+ mockFetch . mockImplementation ( async ( ) => prometheusResponse ( [ ] ) ) ;
229+
230+ const result = await callTool ( client , 'clickstack_describe_source' , {
231+ sourceId : source . _id . toString ( ) ,
232+ metricName : 'idle_metric' ,
233+ } ) ;
234+
235+ const output = JSON . parse ( getFirstText ( result ) ) ;
236+ expect ( output . labelNames ) . toEqual ( [ ] ) ;
237+ expect ( output . note ) . toContain (
238+ 'No series with the metric name "idle_metric" had samples in the last 24 hours.' ,
239+ ) ;
240+ } ) ;
241+
242+ it ( 'passes metricName to a Prometheus endpoint as a match[] selector' , async ( ) => {
243+ const source = await createPrometheusSource ( ) ;
244+ mockFetch . mockImplementation ( async ( ) => prometheusResponse ( [ 'up' ] ) ) ;
245+
246+ await callTool ( client , 'clickstack_describe_source' , {
247+ sourceId : source . _id . toString ( ) ,
248+ metricName : 'http.server.duration' ,
249+ } ) ;
250+
251+ const urls = mockFetch . mock . calls . map (
252+ ( [ input ] ) => new URL ( String ( input ) ) ,
253+ ) ;
254+ const byPath = ( path : string ) => urls . find ( u => u . pathname === path ) ;
255+ expect ( byPath ( '/api/v1/labels' ) ?. searchParams . get ( 'match[]' ) ) . toBe (
256+ '{__name__="http.server.duration"}' ,
257+ ) ;
258+ expect (
259+ byPath ( '/api/v1/label/__name__/values' ) ?. searchParams . has ( 'match[]' ) ,
260+ ) . toBe ( false ) ;
261+ } ) ;
262+
191263 it ( 'describes a Prometheus endpoint source through its label API' , async ( ) => {
192264 const source = await createPrometheusSource ( ) ;
193265 mockFetch . mockImplementation ( async input =>
@@ -225,6 +297,19 @@ describe('MCP PromQL sources', () => {
225297 }
226298 } ) ;
227299
300+ it ( 'adds no metric-name note when an endpoint has no labels in the window' , async ( ) => {
301+ const source = await createPrometheusSource ( ) ;
302+ mockFetch . mockImplementation ( async ( ) => prometheusResponse ( [ ] ) ) ;
303+
304+ const result = await callTool ( client , 'clickstack_describe_source' , {
305+ sourceId : source . _id . toString ( ) ,
306+ } ) ;
307+
308+ const output = JSON . parse ( getFirstText ( result ) ) ;
309+ expect ( output . labelNames ) . toEqual ( [ ] ) ;
310+ expect ( output . note ) . toBeUndefined ( ) ;
311+ } ) ;
312+
228313 it ( 'still returns the source config when the label lookup fails' , async ( ) => {
229314 const source = await createPrometheusSource ( ) ;
230315 mockFetch . mockRejectedValue ( new Error ( 'fetch failed' ) ) ;
@@ -304,83 +389,24 @@ describe('MCP PromQL sources', () => {
304389 versionSpy = undefined ;
305390 } ) ;
306391
307- it ( "sends the label lookups with the source's table and the connection's credentials" , async ( ) => {
392+ // The handler's label lookups join every series as well (#3648).
393+ it ( 'reads names with SQL even where the handler serves label lookups' , async ( ) => {
308394 stubServerVersion ( [ 26 , 10 , 0 , 0 ] ) ;
309- mockFetch . mockImplementation ( async input => {
310- const { pathname } = new URL ( String ( input ) ) ;
311- if ( pathname . endsWith ( '/format_query' ) ) {
312- return prometheusResponse ( 'up' ) ;
313- }
314- return prometheusResponse (
315- pathname . endsWith ( '/label/__name__/values' )
316- ? [ 'up' ]
317- : [ '__name__' , 'job' ] ,
318- ) ;
319- } ) ;
395+ mockFetch . mockImplementation ( async ( ) => prometheusResponse ( 'up' ) ) ;
320396
321397 const output = await describeAutoSource ( ) ;
322398
323399 expect ( output . source ) . toMatchObject ( {
324400 backend : 'clickhouse' ,
325401 table : `${ DEFAULT_DATABASE } .${ TABLE } ` ,
326402 } ) ;
327- expect ( output . labelNames ) . toEqual ( [ '__name__' , 'job' ] ) ;
328- expect ( output . metricNames ) . toEqual ( [ 'up' ] ) ;
329- const urls = labelLookupUrls ( ) ;
330- expect ( urls . map ( u => u . pathname ) . sort ( ) ) . toEqual ( [
331- `${ CLICKHOUSE_PROMETHEUS_API_PREFIX } /label/__name__/values` ,
332- `${ CLICKHOUSE_PROMETHEUS_API_PREFIX } /labels` ,
403+ expect ( output . metricNames ) . toEqual ( [
404+ 'http_requests_total' ,
405+ 'stale_metric' ,
406+ 'up' ,
333407 ] ) ;
334- for ( const url of urls ) {
335- expect ( url . searchParams . getAll ( 'database' ) ) . toEqual ( [
336- DEFAULT_DATABASE ,
337- ] ) ;
338- expect ( url . searchParams . getAll ( 'table' ) ) . toEqual ( [ TABLE ] ) ;
339- expect ( url . searchParams . get ( 'start' ) ) . toBeTruthy ( ) ;
340- expect ( url . searchParams . get ( 'end' ) ) . toBeTruthy ( ) ;
341- expect ( url . searchParams . get ( 'limit' ) ) . toBe ( '201' ) ;
342- }
343- for ( const [ input , init ] of mockFetch . mock . calls ) {
344- if ( new URL ( String ( input ) ) . pathname . endsWith ( '/format_query' ) ) {
345- continue ;
346- }
347- expect ( init ?. headers ) . toMatchObject ( {
348- 'X-ClickHouse-User' : config . CLICKHOUSE_USER ,
349- 'X-ClickHouse-Key' : config . CLICKHOUSE_PASSWORD ,
350- 'X-ClickHouse-Query-Id' : expect . stringMatching ( / ^ h d x - m c p - / ) ,
351- } ) ;
352- }
353- } ) ;
354-
355- // 26.8's handler answers label lookups with a 400 ("not implemented").
356- it ( 'reads labels with SQL on ClickHouse 26.8, whose handler serves only queries' , async ( ) => {
357- stubServerVersion ( [ 26 , 8 , 36 , 3 ] ) ;
358- mockFetch . mockImplementation ( async ( ) => prometheusResponse ( 'up' ) ) ;
359-
360- const output = await describeAutoSource ( ) ;
361-
362- expect ( output . labelNames ) . toEqual ( [ '__name__' , 'instance' , 'job' ] ) ;
363- expect ( output . metricNames ) . toEqual ( [ 'http_requests_total' , 'up' ] ) ;
364408 expect ( labelLookupUrls ( ) ) . toEqual ( [ ] ) ;
365409 } ) ;
366-
367- // Needs ClickHouse 26.9+, whose handler serves label lookups.
368- it ( 'lists labels from the CI ClickHouse handler' , async ( ) => {
369- mockFetch . mockImplementation ( realFetch ) ;
370-
371- const output = await describeAutoSource ( ) ;
372-
373- expect ( output . labelNames ) . toEqual ( [ '__name__' , 'instance' , 'job' ] ) ;
374- expect ( output . metricNames ) . toEqual ( [ 'http_requests_total' , 'up' ] ) ;
375- expect (
376- labelLookupUrls ( )
377- . map ( u => u . pathname )
378- . sort ( ) ,
379- ) . toEqual ( [
380- `${ CLICKHOUSE_PROMETHEUS_API_PREFIX } /label/__name__/values` ,
381- `${ CLICKHOUSE_PROMETHEUS_API_PREFIX } /labels` ,
382- ] ) ;
383- } ) ;
384410 } ) ;
385411
386412 it ( 'keeps promqlBackend through a describe -> rename -> save round trip' , async ( ) => {
0 commit comments