|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {initializeWebMCPPolyfill, cleanupWebMCPPolyfill} from '@mcp-b/webmcp-polyfill'; |
| 10 | +import type {JsonSchemaForInference} from '../../third_party/@mcp-b/webmcp-types'; |
| 11 | +import {Component, createEnvironmentInjector, EnvironmentInjector} from '../../src/core'; |
| 12 | +import {provideRouter, Router, withExperimentalAutoCleanupInjectors} from '@angular/router'; |
| 13 | +import {provideWebMcpTools} from '../../src/webmcp/provide_tools'; |
| 14 | +import {Execute} from '../../src/webmcp/types'; |
| 15 | +import {TestBed} from '../../testing'; |
| 16 | + |
| 17 | +describe('provideWebMcpTools', () => { |
| 18 | + beforeEach(() => { |
| 19 | + initializeWebMCPPolyfill({installTestingShim: true}); |
| 20 | + }); |
| 21 | + |
| 22 | + afterEach(() => { |
| 23 | + cleanupWebMCPPolyfill(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should register tools when initialized', async () => { |
| 27 | + const execute = jasmine.createSpy<Execute<JsonSchemaForInference>>('execute').and.returnValue({ |
| 28 | + content: [{type: 'text', text: 'Hello!'}], |
| 29 | + }); |
| 30 | + |
| 31 | + const envInjector = createEnvironmentInjector( |
| 32 | + [ |
| 33 | + provideWebMcpTools([ |
| 34 | + { |
| 35 | + name: 'testTool', |
| 36 | + description: 'A test tool', |
| 37 | + inputSchema: {type: 'object', properties: {}}, |
| 38 | + execute, |
| 39 | + }, |
| 40 | + ]), |
| 41 | + ], |
| 42 | + TestBed.inject(EnvironmentInjector), |
| 43 | + ); |
| 44 | + |
| 45 | + expect(globalThis.navigator.modelContextTesting!.listTools()).toEqual([ |
| 46 | + jasmine.objectContaining({name: 'testTool'}), |
| 47 | + ]); |
| 48 | + |
| 49 | + await globalThis.navigator.modelContextTesting!.executeTool('testTool', '{}'); |
| 50 | + expect(execute).toHaveBeenCalledOnceWith({}, jasmine.any(Object)); |
| 51 | + |
| 52 | + envInjector.destroy(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should unregister tools when the injector is destroyed', () => { |
| 56 | + const envInjector = createEnvironmentInjector( |
| 57 | + [ |
| 58 | + provideWebMcpTools([ |
| 59 | + { |
| 60 | + name: 'testTool', |
| 61 | + description: 'A test tool', |
| 62 | + inputSchema: {type: 'object', properties: {}}, |
| 63 | + execute: async () => ({content: []}), |
| 64 | + }, |
| 65 | + ]), |
| 66 | + ], |
| 67 | + TestBed.inject(EnvironmentInjector), |
| 68 | + ); |
| 69 | + |
| 70 | + expect(globalThis.navigator.modelContextTesting!.listTools()).toEqual([ |
| 71 | + jasmine.objectContaining({name: 'testTool'}), |
| 72 | + ]); |
| 73 | + |
| 74 | + envInjector.destroy(); |
| 75 | + |
| 76 | + expect(globalThis.navigator.modelContextTesting!.listTools()).toEqual([]); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should work with route providers', async () => { |
| 80 | + @Component({ |
| 81 | + selector: 'test-comp', |
| 82 | + template: '', |
| 83 | + }) |
| 84 | + class TestComp {} |
| 85 | + |
| 86 | + TestBed.configureTestingModule({ |
| 87 | + imports: [TestComp], |
| 88 | + providers: [ |
| 89 | + provideRouter( |
| 90 | + [ |
| 91 | + { |
| 92 | + path: 'test', |
| 93 | + component: TestComp, |
| 94 | + providers: [ |
| 95 | + provideWebMcpTools([ |
| 96 | + { |
| 97 | + name: 'routeTool', |
| 98 | + description: 'A route tool', |
| 99 | + inputSchema: {type: 'object', properties: {}}, |
| 100 | + execute: async () => ({content: []}), |
| 101 | + }, |
| 102 | + ]), |
| 103 | + ], |
| 104 | + }, |
| 105 | + ], |
| 106 | + withExperimentalAutoCleanupInjectors(), |
| 107 | + ), |
| 108 | + ], |
| 109 | + }); |
| 110 | + |
| 111 | + const router = TestBed.inject(Router); |
| 112 | + const rootFixture = TestBed.createComponent(TestComp); |
| 113 | + await rootFixture.whenStable(); |
| 114 | + |
| 115 | + // No tools should be registered initially |
| 116 | + expect(globalThis.navigator.modelContextTesting!.listTools()).toEqual([]); |
| 117 | + |
| 118 | + // Navigate to the route to register the tools |
| 119 | + await router.navigateByUrl('/test'); |
| 120 | + expect(globalThis.navigator.modelContextTesting!.listTools()).toEqual([ |
| 121 | + jasmine.objectContaining({name: 'routeTool'}), |
| 122 | + ]); |
| 123 | + |
| 124 | + // Navigate away to destroy route environment injector context |
| 125 | + await router.navigateByUrl('/'); |
| 126 | + expect(globalThis.navigator.modelContextTesting!.listTools()).toEqual([]); |
| 127 | + }); |
| 128 | +}); |
0 commit comments