Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mcp-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
integrationName: integrationName,
apiKey: process.env.AMPERSAND_API_KEY || '',
groupRef: groupRef,
providerWorkspaceRef: '', // Optional workspace reference for multi-workspace providers
};

export type ClientSettings = typeof clientSettings;
Expand All @@ -51,7 +52,7 @@

try {
mcpApp = main();
} catch (error: any) {

Check warning on line 55 in mcp-server/src/index.ts

View workflow job for this annotation

GitHub Actions / lint-and-type-check

Unexpected any. Specify a different type
console.error('Fatal error in trying to initialize MCP server: ', error);
Sentry.captureException(error);
process.exit(1);
Expand Down
25 changes: 23 additions & 2 deletions mcp-server/src/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { providerSchema } from './schemas';
import { ClientSettings } from '.';
import crypto from 'crypto';
import { z } from 'zod';

export async function createStartOAuthTool(
server: Server,
Expand All @@ -13,21 +14,41 @@ export async function createStartOAuthTool(
`Connect to a SaaS tool provider using the Ampersand OAuth flow. The tool will return a clickable link to the OAuth flow for the user to click.`,
{
provider: providerSchema,
providerWorkspaceRef: z
.string()
.optional()
.describe('Optional workspace reference for multi-workspace providers'),
},
async ({ provider }: { provider: string }) => {
async ({
provider,
providerWorkspaceRef,
}: {
provider: string;
providerWorkspaceRef?: string;
}) => {
let oAuthUrl = '';
try {
const consumerRef = crypto.randomUUID();
const groupRef = settings?.groupRef || process.env.AMPERSAND_GROUP_REF;
const projectId = settings?.project || process.env.AMPERSAND_PROJECT_ID;
const finalProviderWorkspaceRef =
settings?.providerWorkspaceRef || providerWorkspaceRef;
const apiKey = settings?.apiKey || '';
const options: RequestInit = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Api-Key': apiKey,
},
body: JSON.stringify({ provider, consumerRef, groupRef, projectId }),
body: JSON.stringify({
provider,
consumerRef,
groupRef,
projectId,
...(finalProviderWorkspaceRef && {
providerWorkspaceRef: finalProviderWorkspaceRef,
}),
}),
};
console.log(
'[START-OAUTH] API request to oauthConnect: ',
Expand Down
12 changes: 9 additions & 3 deletions sdk/lib/adapters/mcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type ClientSettings = {
integrationName: string;
apiKey: string;
groupRef: string;
providerWorkspaceRef?: string;
};

/**
Expand Down Expand Up @@ -310,13 +311,15 @@ export const createStartOAuthTool = async (
startOAuthToolDescription,
startOAuthInputSchema.shape,
async (params: StartOAuthInputType): Promise<MCPResponse> => {
const { provider, groupRef, consumerRef } = params;
const { provider, groupRef, consumerRef, providerWorkspaceRef } = params;
const finalConsumerRef =
consumerRef || Math.random().toString(36).substring(2, 15);
const finalGroupRef = settings?.groupRef || groupRef || '';
const projectId =
settings?.project || process.env.AMPERSAND_PROJECT_ID || '';
let url = '';
const finalProviderWorkspaceRef =
settings?.providerWorkspaceRef || providerWorkspaceRef;

try {
const response = await fetch(
'https://api.withampersand.com/v1/oauth-connect',
Expand All @@ -328,10 +331,13 @@ export const createStartOAuthTool = async (
consumerRef: finalConsumerRef,
groupRef: finalGroupRef,
projectId,
...(finalProviderWorkspaceRef && {
providerWorkspaceRef: finalProviderWorkspaceRef,
}),
}),
},
);
url = await response.text();
const url = await response.text();
return {
content: [
{
Expand Down
Loading