-
Notifications
You must be signed in to change notification settings - Fork 119
test(mcp-integrations): add Kubernetes MCP actions integration coverage #4549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HusneShabbir
wants to merge
4
commits into
redhat-developer:main
Choose a base branch
from
HusneShabbir:test/mcp-integrations-kubernetes-mcp-actions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+303
−1
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b8a38a9
test(mcp-integrations): add Kubernetes MCP actions integration coverage
0ec8c35
test(mcp-integrations): remove unused mockCredentials import
d7056f5
test(mcp-integrations): slim kubernetes MCP integration backend harness
d5dff13
test(mcp-integrations): address PR review on kubernetes MCP tests
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
227 changes: 227 additions & 0 deletions
227
workspaces/mcp-integrations/packages/backend/src/kubernetes-mcp-tools.integration.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,227 @@ | ||
| /* | ||
| * Copyright Red Hat, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { | ||
| mockCredentials, | ||
| mockServices, | ||
| startTestBackend, | ||
| } from '@backstage/backend-test-utils'; | ||
| import mcpPlugin from '@backstage/plugin-mcp-actions-backend'; | ||
| import mcpKubernetesExtrasPlugin from '@red-hat-developer-hub/backstage-plugin-kubernetes-mcp-extras'; | ||
| import { Client } from '@modelcontextprotocol/sdk/client/index.js'; | ||
| import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'; | ||
| import { | ||
| CallToolResultSchema, | ||
| ListToolsResultSchema, | ||
| } from '@modelcontextprotocol/sdk/types.js'; | ||
| import type { Server } from 'node:http'; | ||
|
|
||
| type CallToolResult = { | ||
| structuredContent?: unknown; | ||
| content?: Array<{ type: string; text?: string }>; | ||
| isError?: boolean; | ||
| }; | ||
|
|
||
| type StartKubernetesMcpBackendOptions = { | ||
| pluginSources?: string[]; | ||
| }; | ||
|
|
||
| const MCP_TRANSPORT_RECONNECTION_OPTIONS = { | ||
| initialReconnectionDelay: 0, | ||
| maxReconnectionDelay: 0, | ||
| reconnectionDelayGrowFactor: 1, | ||
| maxRetries: 0, | ||
| } as const; | ||
|
|
||
| const KUBERNETES_TOOL_NAMES = [ | ||
| 'kubernetes-mcp-extras.get-kubernetes-clusters', | ||
| 'kubernetes-mcp-extras.get-kubernetes-resources-for-entity', | ||
| ] as const; | ||
|
|
||
| function getServerPort(server: Server): number { | ||
| const address = server.address(); | ||
| if (typeof address !== 'object' || !address || !('port' in address)) { | ||
| throw new Error('Test backend server address is unavailable'); | ||
| } | ||
| return address.port; | ||
| } | ||
|
|
||
| function createBackendConfig(pluginSources: string[]) { | ||
| return { | ||
| backend: { | ||
| baseUrl: 'http://localhost:7007', | ||
| actions: { | ||
| pluginSources, | ||
| }, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| async function startKubernetesMcpBackend({ | ||
| pluginSources = ['kubernetes-mcp-extras'], | ||
| }: StartKubernetesMcpBackendOptions = {}) { | ||
| return startTestBackend({ | ||
| features: [ | ||
| mcpKubernetesExtrasPlugin, | ||
| mcpPlugin, | ||
| mockServices.rootConfig.factory({ | ||
| data: createBackendConfig(pluginSources), | ||
| }), | ||
| mockServices.auth.factory(), | ||
| mockServices.httpAuth.factory({ | ||
| defaultCredentials: mockCredentials.user('user:default/test'), | ||
| }), | ||
| ], | ||
| }); | ||
| } | ||
|
|
||
| function createMcpTransport(server: Server): StreamableHTTPClientTransport { | ||
| return new StreamableHTTPClientTransport( | ||
| new URL(`http://127.0.0.1:${getServerPort(server)}/api/mcp-actions/v1`), | ||
| { | ||
| reconnectionOptions: { ...MCP_TRANSPORT_RECONNECTION_OPTIONS }, | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| async function closeMcpConnection( | ||
| client: Client, | ||
| transport: StreamableHTTPClientTransport, | ||
| ): Promise<void> { | ||
| try { | ||
| await transport.terminateSession(); | ||
| } catch { | ||
| // MCP servers may respond with 405 when session termination is unsupported. | ||
| } | ||
|
|
||
| await client.close(); | ||
| } | ||
|
|
||
| async function withMcpClient<T>( | ||
| server: Server, | ||
| run: (client: Client) => Promise<T>, | ||
| ): Promise<T> { | ||
| const client = new Client({ | ||
| name: 'kubernetes-mcp-integration-test', | ||
| version: '1.0.0', | ||
| }); | ||
|
|
||
| const transport = createMcpTransport(server); | ||
|
|
||
| try { | ||
| await client.connect(transport); | ||
| return await run(client); | ||
| } finally { | ||
| await closeMcpConnection(client, transport); | ||
| } | ||
| } | ||
|
|
||
| function parseCallToolError(result: unknown): string { | ||
| const callResult = result as CallToolResult; | ||
| const messages: string[] = []; | ||
|
|
||
| if ('content' in callResult && Array.isArray(callResult.content)) { | ||
| for (const item of callResult.content) { | ||
| if (item.type === 'text' && typeof item.text === 'string') { | ||
| messages.push(item.text); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return messages.join('\n'); | ||
| } | ||
|
|
||
| type McpTestBackend = Awaited<ReturnType<typeof startKubernetesMcpBackend>>; | ||
|
|
||
| describe('Kubernetes MCP tools integration', () => { | ||
| let backend: McpTestBackend; | ||
|
|
||
| beforeAll(async () => { | ||
| backend = await startKubernetesMcpBackend(); | ||
| }); | ||
|
|
||
| it('marks kubernetes tools as read-only in MCP metadata', async () => { | ||
| await withMcpClient(backend.server, async client => { | ||
| const result = await client.request( | ||
| { method: 'tools/list' }, | ||
| ListToolsResultSchema, | ||
| ); | ||
|
|
||
| const toolsByName = Object.fromEntries( | ||
| result.tools.map(tool => [tool.name, tool]), | ||
| ); | ||
|
|
||
| for (const toolName of KUBERNETES_TOOL_NAMES) { | ||
| expect(toolsByName[toolName]?.annotations?.readOnlyHint).toBe(true); | ||
| expect(toolsByName[toolName]?.annotations?.destructiveHint).toBe(false); | ||
| expect(toolsByName[toolName]?.inputSchema).toMatchObject({ | ||
| type: 'object', | ||
| }); | ||
| expect( | ||
| toolsByName[toolName]?.description?.trim().length, | ||
| ).toBeGreaterThan(0); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| it('hides kubernetes tools when kubernetes-mcp-extras is not in pluginSources', async () => { | ||
| const filteredBackend = await startKubernetesMcpBackend({ | ||
| pluginSources: [], | ||
| }); | ||
|
|
||
| await withMcpClient(filteredBackend.server, async client => { | ||
| const result = await client.request( | ||
| { method: 'tools/list' }, | ||
| ListToolsResultSchema, | ||
| ); | ||
|
|
||
| const toolNames = result.tools.map(tool => tool.name); | ||
| for (const toolName of KUBERNETES_TOOL_NAMES) { | ||
| expect(toolNames).not.toContain(toolName); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| it('returns validation error when get-kubernetes-resources-for-entity name is missing', async () => { | ||
| await withMcpClient(backend.server, async client => { | ||
| const result = await client.callTool( | ||
| { | ||
| name: 'kubernetes-mcp-extras.get-kubernetes-resources-for-entity', | ||
| arguments: {}, | ||
| }, | ||
| CallToolResultSchema, | ||
| ); | ||
|
|
||
| expect(result.isError).toBe(true); | ||
| expect(parseCallToolError(result)).toContain('name'); | ||
| }); | ||
| }); | ||
|
|
||
| it('returns validation error when get-kubernetes-resources-for-entity name has invalid type', async () => { | ||
| await withMcpClient(backend.server, async client => { | ||
| const result = await client.callTool( | ||
| { | ||
| name: 'kubernetes-mcp-extras.get-kubernetes-resources-for-entity', | ||
| arguments: { name: 12345 }, | ||
| }, | ||
| CallToolResultSchema, | ||
| ); | ||
|
|
||
| expect(result.isError).toBe(true); | ||
| expect(parseCallToolError(result)).toContain('name'); | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
workspaces/mcp-integrations/plugins/kubernetes-mcp-extras/src/plugin.integration.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /* | ||
| * Copyright Red Hat, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { mockServices, startTestBackend } from '@backstage/backend-test-utils'; | ||
| import { createServiceFactory } from '@backstage/backend-plugin-api'; | ||
| import { actionsRegistryServiceRef } from '@backstage/backend-plugin-api/alpha'; | ||
| import { mcpKubernetesExtrasPlugin } from './plugin'; | ||
|
|
||
| const EXPECTED_ACTIONS = [ | ||
| 'get-kubernetes-clusters', | ||
| 'get-kubernetes-resources-for-entity', | ||
| ]; | ||
|
|
||
| describe('mcpKubernetesExtrasPlugin integration', () => { | ||
| let registeredActionNames: string[]; | ||
|
|
||
| beforeAll(async () => { | ||
| registeredActionNames = []; | ||
|
|
||
| await startTestBackend({ | ||
| features: [ | ||
| mcpKubernetesExtrasPlugin, | ||
| mockServices.rootLogger.factory(), | ||
| mockServices.rootConfig.factory({ | ||
| data: { | ||
| backend: { baseUrl: 'http://localhost:7007' }, | ||
| }, | ||
| }), | ||
| mockServices.auth.factory(), | ||
| mockServices.discovery.factory(), | ||
| createServiceFactory({ | ||
| service: actionsRegistryServiceRef, | ||
| deps: {}, | ||
| factory: () => ({ | ||
| register: (opts: { name: string }) => { | ||
| registeredActionNames.push(opts.name); | ||
| }, | ||
| }), | ||
| }), | ||
| ], | ||
| }); | ||
| }); | ||
|
|
||
| it('registers all expected MCP actions', () => { | ||
| const sortedRegistered = [...registeredActionNames].sort((a, b) => | ||
| a.localeCompare(b), | ||
| ); | ||
| const sortedExpected = [...EXPECTED_ACTIONS].sort((a, b) => | ||
| a.localeCompare(b), | ||
| ); | ||
|
|
||
| expect(sortedRegistered).toEqual(sortedExpected); | ||
| expect(registeredActionNames).toHaveLength(EXPECTED_ACTIONS.length); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.