import { makeOAuthConsent } from './app';
import { McpAgent } from 'agents/mcp';
import OAuthProvider from '@cloudflare/workers-oauth-provider';
import { McpOptions, initMcpServer, server, ClientOptions } from 'dodopayments-mcp/server';
import type { ExportedHandler } from '@cloudflare/workers-types';
type MCPProps = {
clientProps: ClientOptions;
clientConfig: McpOptions;
};
/**
* The information displayed on the OAuth consent screen
*/
const serverConfig: ServerConfig = {
orgName: 'DodoPayments',
instructionsUrl: undefined, // Set a url for where you show users how to get an API key
logoUrl: undefined, // Set a custom logo url to appear during the OAuth flow
clientProperties: [
{
key: 'bearerToken',
label: 'Bearer Token',
description: 'Bearer Token for API authentication',
required: true,
default: undefined,
placeholder: 'My Bearer Token',
type: 'password',
},
{
key: 'webhookKey',
label: 'Webhook Key',
description: '',
required: false,
default: null,
placeholder: 'My Webhook Key',
type: 'string',
},
{
key: 'environment',
label: 'Environment',
description: 'The environment to use for the client',
required: false,
default: 'live_mode',
placeholder: 'live_mode',
type: 'select',
options: [
{ label: 'live_mode', value: 'live_mode' },
{ label: 'test_mode', value: 'test_mode' },
],
},
],
};
export class MyMCP extends McpAgent<Env, unknown, MCPProps> {
server = server;
async init() {
initMcpServer({
server: this.server,
clientOptions: this.props.clientProps,
mcpOptions: this.props.clientConfig,
});
}
}
export type ServerConfig = {
/**
* The name of the company/project
*/
orgName: string;
/**
* An optional company logo image
*/
logoUrl?: string;
/**
* An optional URL with instructions for users to get an API key
*/
instructionsUrl?: string;
/**
* Properties collected to initialize the client
*/
clientProperties: ClientProperty[];
};
export type ClientProperty = {
key: string;
label: string;
description?: string;
required: boolean;
default?: unknown;
placeholder?: string;
type: 'string' | 'number' | 'password' | 'select';
options?: { label: string; value: string }[];
};
// Export the OAuth handler as the default
export default new OAuthProvider({
apiHandlers: {
// @ts-expect-error
'/sse': MyMCP.serveSSE('/sse'), // legacy SSE
// @ts-expect-error
'/mcp': MyMCP.serve('/mcp'), // Streaming HTTP
},
// Type assertion needed due to Headers type mismatch between Hono and @cloudflare/workers-types
// At runtime, Hono's fetch handler is fully compatible with ExportedHandler
defaultHandler: makeOAuthConsent(serverConfig) as unknown as ExportedHandler,
authorizeEndpoint: '/authorize',
tokenEndpoint: '/token',
clientRegistrationEndpoint: '/register',
});