-
Notifications
You must be signed in to change notification settings - Fork 4
feat: add ably event publishing for mcp worker installs #499
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0900e06
feat: add ably event publishing for mcp worker installs
jonathannorris fb64012
fix: updates from PR comments
jonathannorris 12c76fb
refactor: simplify ably event publishing
jonathannorris 67702cc
test: add worker Ably publish tests and vitest setup
jonathannorris 311a456
chore: pin estree types, remove test tsconfig, set TS_NODE_PROJECT, e…
jonathannorris f6bb3e4
feat: update wrangler envs
jonathannorris 15a9366
fix: calling publishMCPInstallEvent() in waitUntil()
jonathannorris 07b18bf
chore: fix TS building issues
jonathannorris 28904b7
test: update Ably MCP error message expectations
jonathannorris 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
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 |
|---|---|---|
| @@ -1,25 +1,30 @@ | ||
| { | ||
| "name": "@devcycle/mcp-worker", | ||
| "version": "1.0.0", | ||
| "private": true, | ||
| "type": "module", | ||
| "scripts": { | ||
| "dev": "wrangler dev", | ||
| "deploy": "wrangler deploy", | ||
| "deploy:dev": "wrangler deploy --env dev", | ||
| "build": "tsc", | ||
| "type-check": "tsc --noEmit", | ||
| "cf-typegen": "wrangler types" | ||
| }, | ||
| "dependencies": { | ||
| "@cloudflare/workers-oauth-provider": "^0.0.5", | ||
| "agents": "^0.0.111", | ||
| "hono": "^4.8.12", | ||
| "jose": "^6.0.12", | ||
| "oauth4webapi": "^3.6.1" | ||
| }, | ||
| "devDependencies": { | ||
| "wrangler": "^4.28.0" | ||
| }, | ||
| "packageManager": "yarn@4.9.2" | ||
| "name": "@devcycle/mcp-worker", | ||
| "version": "1.0.0", | ||
| "private": true, | ||
| "type": "module", | ||
| "scripts": { | ||
| "dev": "wrangler dev --env dev", | ||
| "deploy:prod": "wrangler deploy --env prod", | ||
| "deploy:dev": "wrangler deploy --env dev", | ||
| "build": "tsc", | ||
| "type-check": "tsc --noEmit", | ||
| "cf-typegen": "wrangler types", | ||
| "test": "vitest run", | ||
| "test:watch": "vitest" | ||
| }, | ||
| "dependencies": { | ||
| "@cloudflare/workers-oauth-provider": "^0.0.5", | ||
| "ably": "^1.2.48", | ||
| "agents": "^0.0.111", | ||
| "hono": "^4.8.12", | ||
| "jose": "^6.0.12", | ||
| "oauth4webapi": "^3.6.1" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/node": "^24.3.0", | ||
| "vitest": "^3.2.4", | ||
| "wrangler": "^4.31.0" | ||
| }, | ||
| "packageManager": "yarn@4.9.2" | ||
| } |
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,109 @@ | ||
| import { afterEach, describe, expect, test, vi } from 'vitest' | ||
|
|
||
| // Lock Ably import and shape | ||
| vi.mock('ably/build/ably-webworker.min', () => { | ||
| const publish = vi.fn() | ||
| const channelsGet = vi.fn(() => ({ publish })) | ||
| const channels = { get: channelsGet } | ||
|
|
||
| class RestPromiseMock { | ||
| public static Promise = vi.fn(() => new RestPromiseMock()) | ||
| public channels = channels | ||
| } | ||
|
|
||
| return { default: { Rest: RestPromiseMock } } | ||
| }) | ||
|
|
||
| import Ably from 'ably/build/ably-webworker.min' | ||
| import { publishMCPInstallEvent } from './ably' | ||
|
|
||
| const getMocks = () => { | ||
| const Rest = (Ably as any).Rest as { Promise: any } | ||
| const instance = | ||
| Rest.Promise.mock.results[Rest.Promise.mock.results.length - 1]?.value | ||
| const channelsGet = instance?.channels.get as ReturnType<typeof vi.fn> | ||
| const publish = channelsGet?.mock.results[0]?.value.publish as ReturnType< | ||
| typeof vi.fn | ||
| > | ||
| return { Rest, instance, channelsGet, publish } | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| describe('publishMCPInstallEvent', () => { | ||
| test('publishes correct channel and payload', async () => { | ||
| const env = { ABLY_API_KEY: 'key-123' } | ||
| const claims = { | ||
| org_id: 'org_abc', | ||
| email: 'u@example.com', | ||
| name: 'User', | ||
| } | ||
|
|
||
| await publishMCPInstallEvent(env, claims as any) | ||
|
|
||
| const { Rest, instance, channelsGet, publish } = getMocks() | ||
|
|
||
| expect(Rest.Promise).toHaveBeenCalledWith({ key: env.ABLY_API_KEY }) | ||
| expect(instance).toBeDefined() | ||
| expect(channelsGet).toHaveBeenCalledWith('org_abc-mcp-install') | ||
| expect(publish).toHaveBeenCalledWith('mcp-install', { | ||
| org_id: 'org_abc', | ||
| name: 'User', | ||
| email: 'u@example.com', | ||
| }) | ||
| }) | ||
|
|
||
| test('throws when ABLY_API_KEY missing', async () => { | ||
| await expect( | ||
| publishMCPInstallEvent({}, { | ||
| org_id: 'o', | ||
| name: 'N', | ||
| email: 'e', | ||
| } as any), | ||
| ).rejects.toThrow('ABLY_API_KEY is required to publish Ably MCP events') | ||
| }) | ||
|
|
||
| test('throws when org_id missing', async () => { | ||
| await expect( | ||
| publishMCPInstallEvent({ ABLY_API_KEY: 'k' }, { | ||
| name: 'N', | ||
| email: 'e', | ||
| } as any), | ||
| ).rejects.toThrow( | ||
| 'org_id is required in claims to publish Ably MCP events', | ||
| ) | ||
| }) | ||
|
|
||
| test('logs and rethrows on publish error', async () => { | ||
| const consoleErrorSpy = vi | ||
| .spyOn(console, 'error') | ||
| .mockImplementation(() => {}) | ||
|
|
||
| const env = { ABLY_API_KEY: 'key-123' } | ||
| const claims = { | ||
| org_id: 'org_abc', | ||
| email: 'u@example.com', | ||
| name: 'User', | ||
| } | ||
|
|
||
| // Arrange the mock chain to throw on publish | ||
| const Rest = (await import('ably/build/ably-webworker.min')).default | ||
| .Rest as any | ||
| const instance = Rest.Promise() | ||
| const channelsGet = vi.spyOn(instance.channels, 'get') | ||
| channelsGet.mockReturnValue({ | ||
| publish: vi.fn(async () => { | ||
| throw new Error('boom') | ||
| }), | ||
| }) | ||
|
|
||
| await expect( | ||
| publishMCPInstallEvent(env, claims as any), | ||
| ).rejects.toThrow('boom') | ||
| expect(consoleErrorSpy).toHaveBeenCalled() | ||
|
|
||
| consoleErrorSpy.mockRestore() | ||
| }) | ||
| }) |
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,41 @@ | ||
| import Ably from 'ably/build/ably-webworker.min' | ||
| import type { DevCycleJWTClaims } from './types' | ||
|
|
||
| export async function publishMCPInstallEvent( | ||
| env: { ABLY_API_KEY?: string }, | ||
| claims: DevCycleJWTClaims, | ||
| ): Promise<void> { | ||
| if (!env.ABLY_API_KEY) { | ||
| throw new Error('ABLY_API_KEY is required to publish Ably MCP events') | ||
| } | ||
| if (!claims.org_id) { | ||
| throw new Error( | ||
| 'org_id is required in claims to publish Ably MCP events', | ||
| ) | ||
| } | ||
|
|
||
| const channel = `${claims.org_id}-mcp-install` | ||
|
|
||
| try { | ||
| const ably = new Ably.Rest.Promise({ key: env.ABLY_API_KEY }) | ||
JamieSinn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const ablyChannel = ably.channels.get(channel) | ||
| const payload = { | ||
| org_id: claims.org_id, | ||
| name: claims.name, | ||
| email: claims.email, | ||
| } | ||
| await ablyChannel.publish('mcp-install', payload) | ||
| console.log( | ||
| `Successfully published "mcp-install" event to Ably channel: ${channel}`, | ||
| ) | ||
| } catch (error) { | ||
| console.error('Failed to publish ably "mcp-install" event', { | ||
| error: | ||
| error instanceof Error | ||
| ? { message: error.message } | ||
| : { message: String(error) }, | ||
| channel, | ||
| }) | ||
| throw error | ||
| } | ||
| } | ||
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
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
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.