Skip to content

Commit 99d3b12

Browse files
Unify data structures to eliminate complexity
- Remove rawFormat parameter from listPrimitives - single consistent return format - Eliminate buildPrimitivesArray helper function - Interactive mode now directly iterates over data.tools, data.prompts, etc. - Single source of truth: {capabilities, tools, prompts, resources, resourceTemplates} - Maintains exact same functionality with simpler architecture - No more dual-mode complexity or unnecessary data transformations 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 0b095f6 commit 99d3b12

File tree

1 file changed

+47
-49
lines changed

1 file changed

+47
-49
lines changed

src/mcp.js

Lines changed: 47 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function shouldInclude(filter, type) {
4545
return !filter || filter === type || filter === 'all'
4646
}
4747

48-
async function listPrimitives(client, filter = null, rawFormat = false) {
48+
async function listPrimitives(client, filter = null) {
4949
const capabilities = client.getServerCapabilities()
5050
const promises = []
5151

@@ -78,52 +78,27 @@ async function listPrimitives(client, filter = null, rawFormat = false) {
7878
rawData[type] = data
7979
})
8080

81-
if (rawFormat) {
82-
// Always return consistent structure with empty arrays for unfiltered types
83-
const result = {
84-
capabilities: rawData.capabilities,
85-
tools: [],
86-
prompts: [],
87-
resources: [],
88-
resourceTemplates: []
89-
}
90-
91-
if (filter === 'all' || filter === 'tools') {
92-
result.tools = rawData.tools
93-
}
94-
if (filter === 'all' || filter === 'prompts') {
95-
result.prompts = rawData.prompts
96-
}
97-
if (filter === 'all' || filter === 'resources') {
98-
result.resources = rawData.resources
99-
result.resourceTemplates = rawData.resourceTemplates
100-
}
101-
102-
return result
81+
// Always return consistent structure with empty arrays for unfiltered types
82+
const result = {
83+
capabilities: rawData.capabilities,
84+
tools: [],
85+
prompts: [],
86+
resources: [],
87+
resourceTemplates: []
10388
}
104-
105-
// Build primitives array from rawData for interactive mode
106-
const primitives = []
10789

108-
if (shouldInclude(filter, 'tools') && rawData.tools.length > 0) {
109-
rawData.tools.forEach((item) => primitives.push({ type: 'tool', value: item }))
90+
if (filter === 'all' || filter === 'tools') {
91+
result.tools = rawData.tools
11092
}
111-
112-
if (shouldInclude(filter, 'resources')) {
113-
rawData.resources.forEach((item) => primitives.push({ type: 'resource', value: item }))
114-
rawData.resourceTemplates.forEach((item) =>
115-
primitives.push({
116-
type: 'resource-template',
117-
value: item,
118-
})
119-
)
93+
if (filter === 'all' || filter === 'prompts') {
94+
result.prompts = rawData.prompts
12095
}
121-
122-
if (shouldInclude(filter, 'prompts') && rawData.prompts.length > 0) {
123-
rawData.prompts.forEach((item) => primitives.push({ type: 'prompt', value: item }))
96+
if (filter === 'all' || filter === 'resources') {
97+
result.resources = rawData.resources
98+
result.resourceTemplates = rawData.resourceTemplates
12499
}
125-
126-
return primitives
100+
101+
return result
127102
}
128103

129104
// Command mapping for list operations
@@ -140,7 +115,7 @@ async function executeListCommand(client, command, options = {}) {
140115
if (!filter) {
141116
throw new Error(`Unknown list command: ${command}`)
142117
}
143-
return await listPrimitives(client, filter, true)
118+
return await listPrimitives(client, filter)
144119
}
145120

146121
async function connectServer(transport, options = {}) {
@@ -155,20 +130,43 @@ async function connectServer(transport, options = {}) {
155130
throw err
156131
}
157132

158-
const primitives = await listPrimitives(client)
133+
const data = await listPrimitives(client)
159134
spinner.success(`Connected, server capabilities: ${Object.keys(client.getServerCapabilities()).join(', ')}`)
160135

136+
// Build choices array from the consistent data structure
137+
const choices = []
138+
data.tools.forEach((item) => choices.push({
139+
type: 'tool',
140+
value: item,
141+
title: colors.bold('tool(' + item.name + ')'),
142+
description: formatDescription(item.description, options.compact)
143+
}))
144+
data.resources.forEach((item) => choices.push({
145+
type: 'resource',
146+
value: item,
147+
title: colors.bold('resource(' + item.name + ')'),
148+
description: formatDescription(item.description, options.compact)
149+
}))
150+
data.resourceTemplates.forEach((item) => choices.push({
151+
type: 'resource-template',
152+
value: item,
153+
title: colors.bold('resource-template(' + item.name + ')'),
154+
description: formatDescription(item.description, options.compact)
155+
}))
156+
data.prompts.forEach((item) => choices.push({
157+
type: 'prompt',
158+
value: item,
159+
title: colors.bold('prompt(' + item.name + ')'),
160+
description: formatDescription(item.description, options.compact)
161+
}))
162+
161163
while (true) {
162164
const { primitive } = await prompts(
163165
{
164166
name: 'primitive',
165167
type: 'autocomplete',
166168
message: 'Pick a primitive',
167-
choices: primitives.map((p) => ({
168-
title: colors.bold(p.type + '(' + p.value.name + ')'),
169-
description: formatDescription(p.value.description, options.compact),
170-
value: p,
171-
})),
169+
choices: choices,
172170
},
173171
{
174172
onCancel: async () => {

0 commit comments

Comments
 (0)