Skip to content
Draft
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
44 changes: 21 additions & 23 deletions mcp-server/src/connection.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SDKNodePlatform } from '@amp-labs/sdk-node-platform';
import { z } from 'zod';
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { logger } from '@amp-labs/ai/mcp';
import { providerSchema } from './schemas';
import { ClientSettings } from '.';

Expand All @@ -27,13 +28,13 @@ export async function createConnectionManagerTools(
provider: provider,
};
const data = await ampersandClient.connections.list(body);
console.log('[CHECK-CONNECTION] API call to listConnections: ', body);
logger.info('[CHECK-CONNECTION] API call to listConnections: ', body);

// @ts-ignore
if (data.length > 0) {
// @ts-ignore
const connection = data[0];
console.log(
logger.info(
'[CHECK-CONNECTION] API response from listConnections:',
connection,
);
Expand All @@ -56,7 +57,7 @@ export async function createConnectionManagerTools(
};
}
} catch (err) {
console.error('Error checking connection:', err);
logger.error('[CHECK-CONNECTION] Error checking connection:', err);
return {
content: [
{
Expand Down Expand Up @@ -90,13 +91,13 @@ export async function createConnectionManagerTools(
process.env.AMPERSAND_INTEGRATION_NAME ||
'',
};
console.log(
logger.info(
'[CHECK-INSTALLATION] API call to listInstallations: ',
body,
);
const data = await ampersandClient.installations.list(body);

console.log(
logger.info(
'[CHECK-INSTALLATION] API response from listInstallations: ',
data,
);
Expand All @@ -105,7 +106,7 @@ export async function createConnectionManagerTools(
// @ts-ignore
(inst) => inst.connection?.provider === provider.toLowerCase(),
);
console.log(
logger.info(
'[CHECK-INSTALLATION] filtered installations: ',
relevantInstallations,
);
Expand Down Expand Up @@ -135,7 +136,7 @@ export async function createConnectionManagerTools(
};
}
} catch (err) {
console.error('Error checking installation:', err);
logger.error('[CHECK-INSTALLATION] Error checking installation:', err);
return {
content: [
{
Expand Down Expand Up @@ -182,7 +183,7 @@ export async function createConnectionManagerTools(
},
},
};
console.log(
logger.info(
'[CREATE-INSTALLATION] API call to createInstallation: ',
requestBody,
);
Expand All @@ -197,7 +198,7 @@ export async function createConnectionManagerTools(
requestBody,
});

console.log(
logger.info(
'[CREATE-INSTALLATION] API response from createInstallation: ',
data,
);
Expand All @@ -222,7 +223,7 @@ export async function createConnectionManagerTools(
],
};
} catch (err) {
console.error('Error creating installation:', err);
logger.error('[CREATE-INSTALLATION] Error creating installation:', err);
return {
content: [
{
Expand Down Expand Up @@ -257,11 +258,11 @@ export async function ensureInstallation(
settings?.project || process.env.AMPERSAND_PROJECT_ID || '',
provider: provider,
};
console.log('[ENSURE-INSTALLATION] API call to listConnections: ', body);
logger.info('[ENSURE-INSTALLATION] API call to listConnections: ', body);

const connectionData = await ampersandClient.connections.list(body);

console.log(
logger.info(
'[ENSURE-INSTALLATION] API response from listConnections: ',
connectionData,
);
Expand Down Expand Up @@ -300,18 +301,15 @@ export async function ensureInstallation(
inst.connection?.id === connectionId,
);

console.log(
'[ENSURE-INSTALLATION] existing installation check',
logger.info('[ENSURE-INSTALLATION] existing installation check', {
relevantInstallations,
installationData,
);
});

if (relevantInstallations.length === 0) {
console.log(
logger.info(
'[ENSURE-INSTALLATION] No existing installation found, creating one for connection:',
connectionId,
'group:',
groupRef,
{ connectionId, groupRef },
);

const requestBody = {
Expand All @@ -326,7 +324,7 @@ export async function ensureInstallation(
},
};

console.log(
logger.info(
'[ENSURE-INSTALLATION] API request to createInstallation',
requestBody,
);
Expand All @@ -340,15 +338,15 @@ export async function ensureInstallation(
requestBody,
});

console.log(
logger.info(
'[ENSURE-INSTALLATION] API response from createInstallation',
createData,
);

// @ts-ignore
const installationId = createData.installation?.id;
if (installationId) {
console.log(
logger.info(
`[ENSURE-INSTALLATION]Installation created for ${provider}, Installation ID: ${installationId}`,
);
return installationId;
Expand All @@ -358,7 +356,7 @@ export async function ensureInstallation(
);
}
} else {
console.log(
logger.info(
`[ENSURE-INSTALLATION]Installation already exists for ${provider} with ID: ${relevantInstallations[0].id}`,
);
return relevantInstallations[0].id;
Expand Down
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 @@ export const clientSettings = {
integrationName: integrationName,
apiKey: process.env.AMPERSAND_API_KEY || '',
groupRef: groupRef,
providerWorkspaceRef: '', // Optional workspace reference for multi-workspace providers
};

export type ClientSettings = typeof clientSettings;
Expand Down
32 changes: 27 additions & 5 deletions mcp-server/src/oauth.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { logger } from '@amp-labs/ai/mcp';
import { providerSchema } from './schemas';
import { ClientSettings } from '.';
import crypto from 'crypto';
import { z } from 'zod';

export async function createStartOAuthTool(
server: Server,
Expand All @@ -13,23 +15,43 @@ 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(
logger.info(
'[START-OAUTH] API request to oauthConnect: ',
options.body,
);
Expand All @@ -39,10 +61,10 @@ export async function createStartOAuthTool(
options,
);
const data = await response.text();
console.log('[START-OAUTH] API response from oauthConnect: ', data);
logger.info('[START-OAUTH] API response from oauthConnect: ', data);
oAuthUrl = data;
} catch (err) {
console.error(err);
logger.error('[START-OAUTH] Error in oauthConnect', err);
}

return {
Expand Down
21 changes: 10 additions & 11 deletions mcp-server/src/request.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { z } from 'zod';
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { logger } from '@amp-labs/ai/mcp';
import { ensureInstallation } from './connection';
import {
endpointSchema,
Expand Down Expand Up @@ -131,12 +132,11 @@ async function callAmpersandProxy({
fetchOptions.body = JSON.stringify(body);
}

console.log(
'[SEND-REQUEST] API request to proxy: ',
logger.info('[SEND-REQUEST] API request to proxy: ', {
endpoint,
fetchOptions.body,
);
console.log(
body: fetchOptions.body,
});
logger.info(
'[SEND-REQUEST] Full URL: ',
`https://proxy.withampersand.com/${endpoint}`,
);
Expand All @@ -146,11 +146,10 @@ async function callAmpersandProxy({
fetchOptions,
);

console.log(
'[SEND-REQUEST] API response status from proxy: ',
logger.info('[SEND-REQUEST] API response status from proxy: ', {
endpoint,
response.status,
);
status: response.status,
});

const data = await response.text();
return {
Expand All @@ -170,8 +169,8 @@ async function callAmpersandProxy({
],
};
} catch (error) {
console.error(
`Error in ${method === 'GET' ? 'sendReadRequest' : 'sendRequest'} tool`,
logger.error(
`[SEND-REQUEST] Error in ${method === 'GET' ? 'sendReadRequest' : 'sendRequest'} tool`,
error,
);
return {
Expand Down
5 changes: 3 additions & 2 deletions mcp-server/src/search.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { TrieveSDK, ChunkMetadata } from 'trieve-ts-sdk';
import { z } from 'zod';
import { logger } from '@amp-labs/ai/mcp';

export const SUBDOMAIN: string = 'ampersand-24eb5c1a';
export const SERVER_URL: string = 'https://leaves.mintlify.com';
Expand Down Expand Up @@ -96,7 +97,7 @@ export async function createSearchTool(server: Server): Promise<void> {
query: z.string(),
},
async ({ query }: { query: string }) => {
console.log('[SEARCH] call: ', query);
logger.info('[SEARCH] call: ', query);
const results = await search(query, config);
const content = results.map((result) => {
const { title, content, link } = result;
Expand All @@ -113,6 +114,6 @@ export async function createSearchTool(server: Server): Promise<void> {
},
);
} catch (error) {
console.warn('Error in registering search tool:', error);
logger.warn('[SEARCH] Error in registering search tool:', error);
}
}
7 changes: 4 additions & 3 deletions mcp-server/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
import { SSEServerTransport } from '@modelcontextprotocol/sdk/server/sse.js';
import express, { Request, Response } from 'express';
import { detect } from 'detect-port';
import { logger } from '@amp-labs/ai/mcp';

const DEFAULT_PORT = 3001;
const SSE_SERVER_VERSION = 'v1';
Expand Down Expand Up @@ -50,7 +51,7 @@ export async function connectServer(
settings: any,
): Promise<express.Application | undefined> {
if (useStdioTransport) {
console.log('Connecting to MCP server over stdio');
logger.info('Connecting to MCP server over stdio');
const transport = new StdioServerTransport();
await server.connect(transport);
return;
Expand Down Expand Up @@ -85,7 +86,7 @@ export async function connectServer(
// VS code supports it for example like this: https://github.com/microsoft/vscode-docs/blob/74c4fd5aa3180b218fc389184659b621f05460ca/docs/copilot/chat/mcp-servers.md#configuration-example
settings.apiKey = req.headers['x-api-key'];
}
console.log('[SESSION] Settings: ', settings);
logger.info('[SESSION] Settings: ', settings);
currentTransport = new SSEServerTransport('/messages', res);
transportManager.addTransport(currentTransport, res);
await server.connect(currentTransport);
Expand All @@ -94,7 +95,7 @@ export async function connectServer(
app.post('/messages', async (req: Request, res: Response) => {
const sessionId = req.query.sessionId as string;

console.log('[SESSION] Session ID', sessionId);
logger.info('[SESSION] Session ID', sessionId);
if (!sessionId) {
res.status(400).json({ error: 'Missing session ID param' });
return;
Expand Down
Loading
Loading