Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions webmcp/declarative/toolchange-on-attribute-mutation.https.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>WebMCP: toolchange event and getTools() update on form attribute mutation</title>
<link rel="author" href="mailto:dom@chromium.org">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../resources/helpers.js"></script>
<body>

<form id="f1" toolname="my_tool" tooltitle="My Title" tooldescription="desc">
<input id="input1" type="text" name="input_name">
</form>

<script>
function waitForToolChange() {
return new Promise(resolve => {
navigator.modelContext.addEventListener('toolchange', resolve, { once: true });
});
}

promise_test(async t => {
const form = document.getElementById('f1');

// Wait for initial registration to complete.
await waitForTool('my_tool');

let tools = await navigator.modelContext.getTools();
assert_equals(tools.length, 1, "Initially registered tool");
assert_equals(tools[0].name, 'my_tool');
assert_equals(tools[0].title, 'My Title');
assert_equals(tools[0].description, 'desc');

// Test updating tooltitle attribute
{
const changePromise = waitForToolChange();
form.setAttribute('tooltitle', 'New Title');
await changePromise;

let tools = await navigator.modelContext.getTools();
assert_equals(tools.length, 1, "Still one tool");
assert_equals(tools[0].title, 'New Title', "Title is updated");
}

// Test updating tooldescription attribute
{
const changePromise = waitForToolChange();
form.setAttribute('tooldescription', 'New Description');
await changePromise;

let tools = await navigator.modelContext.getTools();
assert_equals(tools.length, 1);
assert_equals(tools[0].description, 'New Description', "Description is updated");
}

// Test updating toolname attribute
{
const changePromise = waitForToolChange();
form.setAttribute('toolname', 'new_tool_name');
await changePromise;

let tools = await navigator.modelContext.getTools();
assert_equals(tools.length, 1);
assert_equals(tools[0].name, 'new_tool_name', "Name is updated");
}

// Test removing tooltitle attribute (should fall back to empty string)
{
const changePromise = waitForToolChange();
form.removeAttribute('tooltitle');
await changePromise;

let tools = await navigator.modelContext.getTools();
assert_equals(tools.length, 1);
assert_equals(tools[0].title, '', "Title should fall back to empty string");
}

// Test removing tooldescription attribute (should unregister the tool since it's required)
{
const changePromise = waitForToolChange();
form.removeAttribute('tooldescription');
await changePromise;

let tools = await navigator.modelContext.getTools();
assert_equals(tools.length, 0, "Tool should be unregistered when description is removed");
}

// Restore the description attribute to re-register the tool
{
const changePromise = waitForToolChange();
form.setAttribute('tooldescription', 'Restored Description');
await changePromise;

let tools = await navigator.modelContext.getTools();
assert_equals(tools.length, 1);
assert_equals(tools[0].name, 'new_tool_name');
assert_equals(tools[0].description, 'Restored Description');
}

// Test removing toolname attribute (should unregister the tool since it's required)
{
const changePromise = waitForToolChange();
form.removeAttribute('toolname');
await changePromise;

let tools = await navigator.modelContext.getTools();
assert_equals(tools.length, 0, "Tool should be unregistered when name is removed");
}
}, "Test that mutating or removing form attributes (toolname, tooldescription, tooltitle) fires toolchange and updates getTools() correctly");
</script>
</body>
60 changes: 60 additions & 0 deletions webmcp/imperative/register-tool-title.https.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<!DOCTYPE html>
<html>
<head>
<title>Tool title is exposed correctly</title>
<meta name="timeout" content="long">
<link rel="author" href="mailto:dom@chromium.org">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="resources/helpers.js"></script>
</head>
<body>
<script>
promise_test(async () => {
navigator.modelContext.registerTool({
name: 'with-title',
title: 'My Custom Title',
description: 'tool with title',
execute: async () => 'empty'
});

const tools = await navigator.modelContext.getTools();
const tool = tools.find(t => t.name === 'with-title');

assert_equals(tool.title, 'My Custom Title', 'Title is preserved');
}, 'Tool title is preserved when provided');

promise_test(async () => {
navigator.modelContext.registerTool({
name: 'without-title',
description: 'tool without title',
execute: async () => 'empty'
});

const tools = await navigator.modelContext.getTools();
const tool = tools.find(t => t.name === 'without-title');

// If not provided upon registration, `title` should be empty string in the
// dictionary.
assert_equals(tool.title, '', 'Title is empty string when not provided');
}, 'Missing tool title in registration is exposed as empty string');

promise_test(async () => {
// \uD800 is an unpaired high surrogate.
navigator.modelContext.registerTool({
name: 'with-unpaired-surrogate',
title: 'Title with \uD800 unpaired surrogate',
description: 'tool with unpaired surrogate in title',
execute: async () => 'empty'
});

const tools = await navigator.modelContext.getTools();
const tool = tools.find(t => t.name === 'with-unpaired-surrogate');

// USVString replaces unpaired surrogates with U+FFFD.
assert_equals(tool.title, 'Title with \uFFFD unpaired surrogate');
}, 'Tool title with unpaired surrogate is fixed up by USVString');
</script>
</body>
</html>
Loading