added getMcpClient to app

This commit is contained in:
dswbx
2025-08-15 10:27:05 +02:00
parent deb8aacca4
commit 5c28511fe6
4 changed files with 32 additions and 2 deletions

View File

@@ -136,4 +136,21 @@ describe("App", () => {
// expect async listeners to be executed sync after request // expect async listeners to be executed sync after request
expect(called).toHaveBeenCalled(); expect(called).toHaveBeenCalled();
}); });
test("getMcpClient", async () => {
const app = createApp({
initialConfig: {
server: {
mcp: {
enabled: true,
},
},
},
});
await app.build();
const client = app.getMcpClient();
const res = await client.listTools();
expect(res).toBeDefined();
expect(res?.tools.length).toBeGreaterThan(0);
});
}); });

View File

@@ -1,5 +1,5 @@
import type { CreateUserPayload } from "auth/AppAuth"; import type { CreateUserPayload } from "auth/AppAuth";
import { $console } from "bknd/utils"; import { $console, McpClient } from "bknd/utils";
import { Event } from "core/events"; import { Event } from "core/events";
import type { em as prototypeEm } from "data/prototype"; import type { em as prototypeEm } from "data/prototype";
import { Connection } from "data/connection/Connection"; import { Connection } from "data/connection/Connection";
@@ -268,6 +268,17 @@ export class App<C extends Connection = Connection, Options extends AppOptions =
return new Api({ host: "http://localhost", ...(options ?? {}), fetcher }); return new Api({ host: "http://localhost", ...(options ?? {}), fetcher });
} }
getMcpClient() {
if (!this.mcp) {
throw new Error("MCP is not enabled");
}
return new McpClient({
url: "http://localhost/mcp",
fetch: this.server.request,
});
}
async onUpdated<Module extends keyof Modules>(module: Module, config: ModuleConfigs[Module]) { async onUpdated<Module extends keyof Modules>(module: Module, config: ModuleConfigs[Module]) {
// if the EventManager was disabled, we assume we shouldn't // if the EventManager was disabled, we assume we shouldn't
// respond to events, such as "onUpdated". // respond to events, such as "onUpdated".

View File

@@ -11,6 +11,8 @@ export {
mcpResource, mcpResource,
getMcpServer, getMcpServer,
stdioTransport, stdioTransport,
McpClient,
type McpClientConfig,
type ToolAnnotation, type ToolAnnotation,
type ToolHandlerCtx, type ToolHandlerCtx,
} from "jsonv-ts/mcp"; } from "jsonv-ts/mcp";

View File

@@ -1,7 +1,7 @@
export const McpIcon = () => ( export const McpIcon = () => (
<svg <svg
fill="currentColor" fill="currentColor"
fill-rule="evenodd" fillRule="evenodd"
height="1em" height="1em"
style={{ flex: "none", lineHeight: "1" }} style={{ flex: "none", lineHeight: "1" }}
viewBox="0 0 24 24" viewBox="0 0 24 24"