Skip to content

Commit 144fbf9

Browse files
authored
chore: only expose one tool (#17)
1 parent 42d0990 commit 144fbf9

File tree

3 files changed

+29
-35
lines changed

3 files changed

+29
-35
lines changed

lib/setModel.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ async function compileModel(path) {
4040
Object.assign(def, info)
4141
}
4242

43+
for (const name in compiled.definitions) {
44+
Object.defineProperty(compiled.definitions[name], 'name', { value: name, enumerable: true })
45+
}
46+
4347
const _entities_in = service => {
4448
const exposed = [],
4549
{ entities } = service

lib/tools.js

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@ import { z } from 'zod'
55
import setModel from './setModel.js'
66
import fuzzyTopN from './fuzzyTopN.js'
77

8-
const PROJECT_PATH = {
9-
projectPath: z.string().describe('Root path of the project')
10-
}
11-
128
const tools = {
139
search_cds_definitions: {
1410
title: 'Search for CDS definitions',
1511
description:
1612
'Get details of CDS definitions, returns Core Schema Notation (CSN). Use this if you want to see elements, parameters, file locations, URL paths, etc., helpful when constructing queries or OData URLs or when modifying CDS models.',
1713
inputSchema: {
18-
...PROJECT_PATH,
19-
name: z.string().optional().describe('Name of the definition (fuzzy search, no regex or special characters)'),
20-
kind: z.string().optional().describe('Kind of the definition (service, entity, action, ...)'),
21-
topN: z.number().default(1).describe('Number of results')
14+
projectPath: z.string().describe('Root path of the project'),
15+
name: z
16+
.string()
17+
.optional()
18+
.describe('Name of the definition (fuzzy search (Levenshtein distance), no regex or special characters)'),
19+
kind: z.string().optional().describe('Filter for kind of the definition (service, entity, action, ...)'),
20+
topN: z.number().default(1).describe('Number of results'),
21+
namesOnly: z.boolean().optional().describe('If true, only return the names of the definitions')
2222
},
23-
handler: async ({ projectPath, name, kind, topN }) => {
23+
handler: async ({ projectPath, name, kind, topN, namesOnly }) => {
2424
await setModel(projectPath)
2525
const defNames = kind
2626
? Object.entries(cds.model.definitions)
@@ -29,26 +29,10 @@ const tools = {
2929
.map(([k]) => k)
3030
: Object.keys(cds.model.definitions)
3131
const scores = name ? fuzzyTopN(name, defNames, topN) : fuzzyTopN('', defNames, topN)
32-
return scores.map(s => Object.assign({ name: s.item }, cds.model.definitions[s.item]))
33-
}
34-
},
35-
list_all_cds_definition_names: {
36-
title: 'List all CDS definitions names',
37-
description:
38-
'Get an overview of available CDS definitions, for details use `search_cds_definitions`. Helpful for initial exploration, e.g. to get all service names.',
39-
inputSchema: {
40-
...PROJECT_PATH,
41-
kind: z.string().optional().describe('Kind of the definition (service, entity, action, ...)')
42-
},
43-
handler: async ({ projectPath, kind }) => {
44-
await setModel(projectPath)
45-
const defNames = kind
46-
? Object.entries(cds.model.definitions)
47-
// eslint-disable-next-line no-unused-vars
48-
.filter(([_k, v]) => v.kind === kind)
49-
.map(([k]) => k)
50-
: Object.keys(cds.model.definitions)
51-
return defNames
32+
if (namesOnly) {
33+
return scores.map(s => s.item)
34+
}
35+
return scores.map(s => cds.model.definitions[s.item])
5236
}
5337
}
5438
}

tests/tools.test.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,27 @@ test.describe('tools', () => {
4040
assert.equal(books[0].endpoints[0].path, 'odata/v4/admin/Books', 'Should contain endpoint path')
4141
})
4242

43-
test('list_all_cds_definition_names: should list all entities', async () => {
44-
const entities = await tools.list_all_cds_definition_names.handler({
43+
test('search_cds_definitions: should list all entities (namesOnly)', async () => {
44+
const entities = await tools.search_cds_definitions.handler({
4545
projectPath: sampleProjectPath,
46-
kind: 'entity'
46+
kind: 'entity',
47+
topN: 100,
48+
namesOnly: true
4749
})
4850
assert(Array.isArray(entities), 'Entities should be an array')
4951
assert(entities.length > 0, 'Should find at least one entity')
52+
assert(typeof entities[0] === 'string', 'Should return only names')
5053
})
5154

52-
test('list_all_cds_definition_names: should list all services', async () => {
53-
const services = await tools.list_all_cds_definition_names.handler({
55+
test('search_cds_definitions: should list all services (namesOnly)', async () => {
56+
const services = await tools.search_cds_definitions.handler({
5457
projectPath: sampleProjectPath,
55-
kind: 'service'
58+
kind: 'service',
59+
topN: 100,
60+
namesOnly: true
5661
})
5762
assert(Array.isArray(services), 'Services should be an array')
5863
assert(services.length > 0, 'Should find at least one service')
64+
assert(typeof services[0] === 'string', 'Should return only names')
5965
})
6066
})

0 commit comments

Comments
 (0)