From e04360f83e9a2937f94d7fc47e1f83c1715f9f4a Mon Sep 17 00:00:00 2001 From: andypalmi Date: Mon, 3 Aug 2026 16:12:33 +0200 Subject: [PATCH 1/2] feat(mcp): shared query schema fragments for platform tools Add forge/ee/lib/mcp/schemas.js, a shared module of composable zod fragments the platform read tools import instead of redefining entity-id and pagination/search/sort/audit-log query fields in each tool file. - entity-id params: teamId, applicationId, hostedInstanceId (UUID), remoteInstanceId, snapshotId - query fragments composed per route by spreading only the params the backing finder honors: cursorParam/limitParam (basePagination), pageParam, searchQuery, sortParams, auditLogFilters - appendQuery serialises a tool's supported params onto the request URL The module lives one level above tools/ so the tool loader does not register it as a tool module. Closes #7669 --- forge/ee/lib/mcp/schemas.js | 92 ++++++++++++++++++++++ test/unit/forge/ee/lib/mcp/schemas_spec.js | 76 ++++++++++++++++++ 2 files changed, 168 insertions(+) create mode 100644 forge/ee/lib/mcp/schemas.js create mode 100644 test/unit/forge/ee/lib/mcp/schemas_spec.js diff --git a/forge/ee/lib/mcp/schemas.js b/forge/ee/lib/mcp/schemas.js new file mode 100644 index 0000000000..03d628a008 --- /dev/null +++ b/forge/ee/lib/mcp/schemas.js @@ -0,0 +1,92 @@ +const { z } = require('zod') + +// Hosted instances are Projects (UUID primary key); the other entities use hashids. +const teamId = z.string().describe('The ID or hashid of the team') +const applicationId = z.string().describe('The ID or hashid of the application') +const hostedInstanceId = z.string().uuid().describe('The UUID of the hosted instance') +const remoteInstanceId = z.string().describe('The ID or hashid of the remote instance') +const snapshotId = z.string().describe('The hashid of the snapshot') + +// Query fragments composed per tool by spreading only the ones the backing +// route's finder actually honors. Not the same as the route's declared query +// schema: most list routes reuse a generic PaginationParams that advertises +// page/sort/dir/order even when the finder ignores them. Match the finder. + +const cursorParam = { + cursor: z.string().optional().describe('Opaque cursor from a previous page') +} +const limitParam = { + limit: z.number().int().min(1).max(50).default(10).describe('Maximum number of records to return (1-50, default 10)') +} +const basePagination = { ...cursorParam, ...limitParam } + +// Only Device.getAll and Project.byTeam read page and compute an offset. +const pageParam = { + page: z.number().int().min(1).optional().default(1).describe('1-based page number (offset pagination)') +} + +const searchQuery = { + query: z.string().optional().describe('Free-text search filter') +} + +// Only Project.byTeam honors sort; no finder reads the legacy `order` alias. +const sortParams = { + sort: z.string().optional().describe('Field name to sort by'), + dir: z.enum(['asc', 'desc']).optional().describe('Sort direction') +} + +// scope is route-specific (its enum differs per entity), so each tool declares it inline. +const auditLogFilters = { + event: z.union([z.string(), z.array(z.string())]).optional().describe('Filter by audit event name, or an array of event names'), + username: z.string().optional().describe('Filter by the username that triggered the event') +} + +const cursorParamKeys = Object.keys(cursorParam) +const limitParamKeys = Object.keys(limitParam) +const basePaginationKeys = Object.keys(basePagination) +const pageParamKeys = Object.keys(pageParam) +const searchQueryKeys = Object.keys(searchQuery) +const sortParamsKeys = Object.keys(sortParams) +const auditLogFilterKeys = Object.keys(auditLogFilters) + +// Serialise the given query keys from args onto a url: only defined values, +// URL-encoded, an array value appended once per element. +function appendQuery (url, args, keys) { + const params = new URLSearchParams() + for (const key of keys) { + const value = args[key] + if (value === undefined || value === null) { + continue + } + if (Array.isArray(value)) { + value.forEach(v => params.append(key, v)) + } else { + params.append(key, value) + } + } + const queryString = params.toString() + return queryString ? `${url}?${queryString}` : url +} + +module.exports = { + teamId, + applicationId, + hostedInstanceId, + remoteInstanceId, + snapshotId, + cursorParam, + limitParam, + basePagination, + pageParam, + searchQuery, + sortParams, + auditLogFilters, + cursorParamKeys, + limitParamKeys, + basePaginationKeys, + pageParamKeys, + searchQueryKeys, + sortParamsKeys, + auditLogFilterKeys, + appendQuery +} diff --git a/test/unit/forge/ee/lib/mcp/schemas_spec.js b/test/unit/forge/ee/lib/mcp/schemas_spec.js new file mode 100644 index 0000000000..ae9d953474 --- /dev/null +++ b/test/unit/forge/ee/lib/mcp/schemas_spec.js @@ -0,0 +1,76 @@ +const should = require('should') // eslint-disable-line no-unused-vars + +const FF_UTIL = require('flowforge-test-utils') + +const { + teamId, + applicationId, + hostedInstanceId, + remoteInstanceId, + snapshotId, + basePagination, + cursorParamKeys, + limitParamKeys, + basePaginationKeys, + pageParamKeys, + searchQueryKeys, + sortParamsKeys, + auditLogFilterKeys, + appendQuery +} = FF_UTIL.require('forge/ee/lib/mcp/schemas') + +describe('MCP shared query schemas', function () { + describe('entity-id params', function () { + it('hashid params accept any non-empty string', function () { + for (const param of [teamId, applicationId, remoteInstanceId, snapshotId]) { + param.parse('aBc123').should.equal('aBc123') + } + }) + it('hostedInstanceId accepts a UUID and rejects a hashid', function () { + hostedInstanceId.parse('4f8c1e2a-1b2c-4d3e-8f90-abcdef123456').should.be.a.String() + hostedInstanceId.safeParse('aBc123').success.should.equal(false) + }) + }) + + describe('fragment composition', function () { + it('basePagination is cursor plus limit only (no page)', function () { + basePaginationKeys.should.eql(['cursor', 'limit']) + }) + it('page, search and sort are separate opt-in fragments', function () { + cursorParamKeys.should.eql(['cursor']) + limitParamKeys.should.eql(['limit']) + pageParamKeys.should.eql(['page']) + searchQueryKeys.should.eql(['query']) + sortParamsKeys.should.eql(['sort', 'dir']) + auditLogFilterKeys.should.eql(['event', 'username']) + }) + it('key list matches the shape object it describes', function () { + basePaginationKeys.should.eql(Object.keys(basePagination)) + }) + }) + + describe('appendQuery', function () { + it('returns the url unchanged when no supported params are set', function () { + appendQuery('/api/v1/x', {}, basePaginationKeys).should.equal('/api/v1/x') + }) + it('returns the url unchanged when only unsupported keys are present', function () { + appendQuery('/api/v1/x', { teamId: 'abc' }, basePaginationKeys).should.equal('/api/v1/x') + }) + it('serialises only the defined, supported params', function () { + appendQuery('/api/v1/x', { limit: 10, page: 2, cursor: undefined }, [...basePaginationKeys, ...pageParamKeys]) + .should.equal('/api/v1/x?limit=10&page=2') + }) + it('url-encodes values', function () { + appendQuery('/api/v1/x', { query: 'a b&c' }, searchQueryKeys) + .should.equal('/api/v1/x?query=a+b%26c') + }) + it('appends an array value once per element', function () { + appendQuery('/api/v1/x', { event: ['flows.created', 'flows.deleted'] }, auditLogFilterKeys) + .should.equal('/api/v1/x?event=flows.created&event=flows.deleted') + }) + it('skips null and undefined but keeps other values', function () { + appendQuery('/api/v1/x', { limit: null, page: 1 }, [...basePaginationKeys, ...pageParamKeys]) + .should.equal('/api/v1/x?page=1') + }) + }) +}) From c10c4cad152851abfb7fda137422da923c35101a Mon Sep 17 00:00:00 2001 From: andypalmi Date: Mon, 3 Aug 2026 17:26:16 +0200 Subject: [PATCH 2/2] feat(mcp): add bill of materials read tools Expose team and application bill-of-materials as read-only MCP tools. --- forge/ee/lib/mcp/tools/bom.js | 40 +++++++++++++ forge/routes/auth/permissions.js | 5 +- test/unit/forge/ee/lib/mcp/tools/bom_spec.js | 60 ++++++++++++++++++++ 3 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 forge/ee/lib/mcp/tools/bom.js create mode 100644 test/unit/forge/ee/lib/mcp/tools/bom_spec.js diff --git a/forge/ee/lib/mcp/tools/bom.js b/forge/ee/lib/mcp/tools/bom.js new file mode 100644 index 0000000000..1999157b13 --- /dev/null +++ b/forge/ee/lib/mcp/tools/bom.js @@ -0,0 +1,40 @@ +const { teamId, applicationId } = require('../schemas') + +module.exports = [ + { + name: 'platform_get_team_bom', + title: 'Get Team Bill of Materials', + description: `FlowFuse platform automation tool: + Reads the bill of materials for a team: the applications, instances, and their + dependencies across the team. This is plan-gated on the bom feature, which defaults + to disabled; if disabled for the team, the tool reports that the bill of materials + is not enabled for this team rather than the raw platform error. + Results are filtered to the applications the calling token can access, + so a scoped token sees only its in-scope subset instead of an error.`, + annotations: { readOnlyHint: true, destructiveHint: false }, + inputSchema: { + teamId + }, + handler: async (args, { inject }) => { + const response = await inject({ method: 'GET', url: `/api/v1/teams/${args.teamId}/bom` }) + return response + } + }, + { + name: 'platform_get_application_bom', + title: 'Get Application Bill of Materials', + description: `FlowFuse platform automation tool: + Reads the bill of materials for a single application: its instances and their dependencies. + This is plan-gated on the bom feature, which defaults to disabled; if the team's plan + has this feature disabled, the tool reports that the bill of materials is not enabled + for this team rather than the raw platform error.`, + annotations: { readOnlyHint: true, destructiveHint: false }, + inputSchema: { + applicationId + }, + handler: async (args, { inject }) => { + const response = await inject({ method: 'GET', url: `/api/v1/applications/${args.applicationId}/bom` }) + return response + } + } +] diff --git a/forge/routes/auth/permissions.js b/forge/routes/auth/permissions.js index c1740abaa9..02cf9a89f6 100644 --- a/forge/routes/auth/permissions.js +++ b/forge/routes/auth/permissions.js @@ -82,7 +82,10 @@ const IMPLICIT_TOKEN_SCOPES = { 'stack:list', 'flow-blueprint:list', 'project:status', - 'template:list' + 'template:list', + // bill of materials + 'team:bom', // get team bill of materials + 'application:bom' // get application bill of materials ] } diff --git a/test/unit/forge/ee/lib/mcp/tools/bom_spec.js b/test/unit/forge/ee/lib/mcp/tools/bom_spec.js new file mode 100644 index 0000000000..a834e5041c --- /dev/null +++ b/test/unit/forge/ee/lib/mcp/tools/bom_spec.js @@ -0,0 +1,60 @@ +const should = require('should') // eslint-disable-line no-unused-vars +const sinon = require('sinon') + +const tools = require('../../../../../../../forge/ee/lib/mcp/tools/bom') + +function getTool (name) { + return tools.find(tool => tool.name === name) +} + +describe('MCP Bill of Materials Tools', function () { + let inject + + beforeEach(function () { + inject = sinon.stub() + }) + + describe('platform_get_team_bom', function () { + const tool = getTool('platform_get_team_bom') + + it('injects the team bom route and returns the response', async function () { + const routeResponse = { statusCode: 200, json: () => ([]) } + inject.withArgs({ method: 'GET', url: '/api/v1/teams/team1/bom' }).resolves(routeResponse) + + const response = await tool.handler({ teamId: 'team1' }, { inject }) + + inject.calledOnce.should.be.true() + response.should.equal(routeResponse) + }) + + it('passes through an error response', async function () { + const errorResponse = { statusCode: 404, json: () => ({ code: 'not_found' }) } + inject.resolves(errorResponse) + + const response = await tool.handler({ teamId: 'team1' }, { inject }) + response.should.equal(errorResponse) + }) + }) + + describe('platform_get_application_bom', function () { + const tool = getTool('platform_get_application_bom') + + it('injects the application bom route and returns the response', async function () { + const routeResponse = { statusCode: 200, json: () => ({}) } + inject.withArgs({ method: 'GET', url: '/api/v1/applications/app1/bom' }).resolves(routeResponse) + + const response = await tool.handler({ applicationId: 'app1' }, { inject }) + + inject.calledOnce.should.be.true() + response.should.equal(routeResponse) + }) + + it('passes through an error response', async function () { + const errorResponse = { statusCode: 404, json: () => ({ code: 'not_found' }) } + inject.resolves(errorResponse) + + const response = await tool.handler({ applicationId: 'app1' }, { inject }) + response.should.equal(errorResponse) + }) + }) +})