Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

2.5 KB
Raw Blame
<!DOCTYPE html>
<html>
<head>
<title>WebMCP failed tool execution does not fire window.onerror</title>
<link rel="author" href="mailto:[email protected]">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<script>
promise_test(async t => {
  let errorFired = false;
  const errorHandler = () => {
    errorFired = true;
  };
  window.addEventListener('error', errorHandler);
  t.add_cleanup(() => {
    window.removeEventListener('error', errorHandler);
  });

  // Register a tool that throws an error when executed.
  await document.modelContext.registerTool({
    name: 'throwing_tool',
    description: 'A tool that throws an error',
    execute: async () => {
      throw new Error('Tool execution failed intentionally');
    }
  });

  const tools = await document.modelContext.getTools();
  const tool = tools.find(t => t.name === 'throwing_tool');
  assert_true(!!tool, 'Should find the registered tool');

  // `executeTool()` should reject since the tool execution failed.
  await promise_rejects_dom(t, 'UnknownError',
      document.modelContext.executeTool(tool),
      'executeTool() rejects when the tool execution throws');

  assert_false(errorFired, 'window.onerror/error event should not be fired');
}, 'Failed tool execution does not trigger window.onerror');

promise_test(async t => {
  let errorFired = false;
  const errorHandler = () => {
    errorFired = true;
  };
  window.addEventListener('error', errorHandler);
  t.add_cleanup(() => {
    window.removeEventListener('error', errorHandler);
  });

  // Register a tool that returns a circular object when executed.
  await document.modelContext.registerTool({
    name: 'circular_tool',
    description: 'A tool that returns a circular object',
    execute: () => {
      let a = {};
      a['a'] = a;
      return a;
    }
  });

  const tools = await document.modelContext.getTools();
  const tool = tools.find(t => t.name === 'circular_tool');
  assert_true(!!tool, 'Should find the registered tool');

  // `executeTool()` should reject since serializing the return value throws a TypeError.
  await promise_rejects_dom(t, 'UnknownError',
      document.modelContext.executeTool(tool),
      'executeTool() rejects when the tool execution returns a circular object');

  assert_false(errorFired, 'window.onerror/error event should not be fired');
}, 'Tool execution returning circular object rejects and does not trigger window.onerror');
</script>
</body>
</html>