Skip to content

Commit 2f269c9

Browse files
authored
Fill default values in test tool (#644)
* default values are populated in test tool form Signed-off-by: Keval Mahajan <[email protected]> * linting Signed-off-by: Keval Mahajan <[email protected]> --------- Signed-off-by: Keval Mahajan <[email protected]>
1 parent ccfc45e commit 2f269c9

File tree

2 files changed

+47
-19
lines changed

2 files changed

+47
-19
lines changed

mcpgateway/admin.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1317,8 +1317,10 @@ async def admin_ui(
13171317
>>> root_service.list_roots = original_list_roots
13181318
"""
13191319
logger.debug(f"User {user} accessed the admin UI")
1320+
tools = [
1321+
tool.model_dump(by_alias=True) for tool in sorted(await tool_service.list_tools(db, include_inactive=include_inactive), key=lambda t: ((t.url or "").lower(), (t.original_name or "").lower()))
1322+
]
13201323
servers = [server.model_dump(by_alias=True) for server in await server_service.list_servers(db, include_inactive=include_inactive)]
1321-
tools = [tool.model_dump(by_alias=True) for tool in await tool_service.list_tools(db, include_inactive=include_inactive)]
13221324
resources = [resource.model_dump(by_alias=True) for resource in await resource_service.list_resources(db, include_inactive=include_inactive)]
13231325
prompts = [prompt.model_dump(by_alias=True) for prompt in await prompt_service.list_prompts(db, include_inactive=include_inactive)]
13241326
gateways = [gateway.model_dump(by_alias=True) for gateway in await gateway_service.list_gateways(db, include_inactive=include_inactive)]
@@ -1464,6 +1466,7 @@ async def admin_list_tools(
14641466
"""
14651467
logger.debug(f"User {user} requested tool list")
14661468
tools = await tool_service.list_tools(db, include_inactive=include_inactive)
1469+
14671470
return [tool.model_dump(by_alias=True) for tool in tools]
14681471

14691472

mcpgateway/static/admin.js

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3307,28 +3307,32 @@ async function testTool(toolId) {
33073307

33083308
// Field label - use textContent to avoid double escaping
33093309
const label = document.createElement("label");
3310-
label.textContent = keyValidation.value;
33113310
label.className =
33123311
"block text-sm font-medium text-gray-700 dark:text-gray-300";
3312+
3313+
// Create span for label text
3314+
const labelText = document.createElement("span");
3315+
labelText.textContent = keyValidation.value;
3316+
label.appendChild(labelText);
3317+
3318+
// Add red star if field is required
3319+
if (schema.required && schema.required.includes(key)) {
3320+
const requiredMark = document.createElement("span");
3321+
requiredMark.textContent = " *";
3322+
requiredMark.className = "text-red-500";
3323+
label.appendChild(requiredMark);
3324+
}
3325+
33133326
fieldDiv.appendChild(label);
33143327

33153328
// Description help text - use textContent
33163329
if (prop.description) {
33173330
const description = document.createElement("small");
3318-
description.textContent = prop.description; // NO escapeHtml here
3331+
description.textContent = prop.description;
33193332
description.className = "text-gray-500 block mb-1";
33203333
fieldDiv.appendChild(description);
33213334
}
33223335

3323-
// Input field with validation
3324-
const input = document.createElement("input");
3325-
input.name = keyValidation.value;
3326-
input.type = "text";
3327-
input.required =
3328-
schema.required && schema.required.includes(key);
3329-
input.className =
3330-
"mt-1 block w-full rounded-md border border-gray-500 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 dark:bg-gray-900 text-gray-700 dark:text-gray-300 dark:border-gray-700 dark:focus:border-indigo-400 dark:focus:ring-indigo-400";
3331-
33323336
if (prop.type === "array") {
33333337
const arrayContainer = document.createElement("div");
33343338
arrayContainer.className = "space-y-2";
@@ -3343,18 +3347,17 @@ async function testTool(toolId) {
33433347
schema.required && schema.required.includes(key);
33443348
input.className =
33453349
"mt-1 block w-full rounded-md border border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 dark:bg-gray-900 text-gray-700 dark:text-gray-300 dark:border-gray-700 dark:focus:border-indigo-400 dark:focus:ring-indigo-400";
3346-
if (prop.items && prop.items.type === "number") {
3350+
3351+
if (prop.items?.type === "number") {
33473352
input.type = "number";
3348-
} else if (
3349-
prop.items &&
3350-
prop.items.type === "boolean"
3351-
) {
3353+
} else if (prop.items?.type === "boolean") {
33523354
input.type = "checkbox";
33533355
input.value = "true";
33543356
input.checked = value === true || value === "true";
33553357
} else {
33563358
input.type = "text";
33573359
}
3360+
33583361
if (
33593362
typeof value === "string" ||
33603363
typeof value === "number"
@@ -3386,7 +3389,13 @@ async function testTool(toolId) {
33863389
arrayContainer.appendChild(createArrayInput());
33873390
});
33883391

3389-
arrayContainer.appendChild(createArrayInput());
3392+
if (Array.isArray(prop.default)) {
3393+
prop.default.forEach((val) => {
3394+
arrayContainer.appendChild(createArrayInput(val));
3395+
});
3396+
} else {
3397+
arrayContainer.appendChild(createArrayInput());
3398+
}
33903399

33913400
fieldDiv.appendChild(arrayContainer);
33923401
fieldDiv.appendChild(addBtn);
@@ -3401,19 +3410,35 @@ async function testTool(toolId) {
34013410
// Add validation based on type
34023411
if (prop.type === "text") {
34033412
input.type = "text";
3404-
} else if (prop.type === "number") {
3413+
} else if (
3414+
prop.type === "number" ||
3415+
prop.type === "integer"
3416+
) {
34053417
input.type = "number";
34063418
} else if (prop.type === "boolean") {
34073419
input.type = "checkbox";
34083420
input.className =
34093421
"mt-1 h-4 w-4 text-indigo-600 dark:text-indigo-200 border border-gray-300 rounded";
3422+
} else {
3423+
input.type = "text";
34103424
}
3425+
3426+
// Set default values here
3427+
if (prop.default !== undefined) {
3428+
if (input.type === "checkbox") {
3429+
input.checked = prop.default === true;
3430+
} else {
3431+
input.value = prop.default;
3432+
}
3433+
}
3434+
34113435
fieldDiv.appendChild(input);
34123436
}
34133437

34143438
container.appendChild(fieldDiv);
34153439
}
34163440
}
3441+
34173442
openModal("tool-test-modal");
34183443
console.log("✓ Tool test modal loaded successfully");
34193444
} catch (error) {

0 commit comments

Comments
 (0)