AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents
TypeScript ai-agentai-agent-toolsai-agentsai-agents-frameworkmcpmcp-servermcp-toolsmcpsn8n-alternativeno-code-automationworkflowworkflow-automationworkflows
import { isNil } from '@activepieces/core-utils'
import { FastifyPluginAsyncZod } from 'fastify-type-provider-zod'
import { z } from 'zod'
import { securityAccess } from '../../../core/security/authorization/fastify-security'
import { domainHelper } from '../../../helper/domain-helper'
import { JwtAudience, jwtUtils } from '../../../helper/jwt-utils'
import { networkUtils } from '../../../helper/network-utils'
import { mcpOAuthClientService } from '../client/mcp-oauth-client.service'
import { DEFAULT_MCP_OAUTH_SCOPES } from '../mcp-oauth-scopes'
import { mcpOAuthValidation } from '../mcp-oauth-validation'
const AUTH_REQUEST_TTL_SECONDS = 30 * 60
export const mcpOAuthAuthorizeController: FastifyPluginAsyncZod = async (app) => {
app.get('/authorize', AuthorizeRequest, async (req, reply) => {
const { client_id, redirect_uri, response_type, code_challenge, code_challenge_method, state, scope, resource, nonce } = req.query
if (response_type !== 'code') {
return reply.status(400).send({ error: 'unsupported_response_type' })
}
if (code_challenge_method !== 'S256') {
return reply.status(400).send({ error: 'invalid_request', error_description: 'Only S256 code challenge method is supported' })
}
const client = await mcpOAuthClientService.getByClientId(client_id)
if (isNil(client)) {
return reply.status(400).send({ error: 'invalid_client', error_description: 'Unknown client_id' })
}
if (!mcpOAuthClientService.validateRedirectUri(client, redirect_uri)) {
return reply.status(400).send({ error: 'invalid_request', error_description: 'Invalid redirect_uri' })
}
const key = await jwtUtils.getJwtSecret()
const authRequestToken = await jwtUtils.sign({
payload: {
clientId: client_id,
clientName: client.clientName ?? 'Unknown app',
redirectUri: redirect_uri,
codeChallenge: code_challenge,
codeChallengeMethod: code_challenge_method,
state: state ?? null,
nonce: nonce ?? null,
scopes: scope ? scope.split(' ') : DEFAULT_MCP_OAUTH_SCOPES,
resource: resource ?? null,
type: 'mcp_auth_request',
},
key,
expiresInSeconds: AUTH_REQUEST_TTL_SECONDS,
audience: JwtAudience.MCP_OAUTH_AUTH_REQUEST,
})
if (domainHelper.hasMcpUrl() && domainHelper.isUnconfiguredHostRequest({ req })) {
req.log.warn({ requestHost: networkUtils.getRequestHost(req) }, '[mcpOAuthAuthorizeController] Request host matches neither AP_MCP_URL nor AP_FRONTEND_URL; the proxy must forward the public hostname in X-Forwarded-Host or keep the Host header')
}
const consentPageUrl = domainHelper.isMcpHostRequest({ req })
? domainHelper.getBrowserLandingUrl({ path: '/mcp-authorize' })
: networkUtils.combineUrl(networkUtils.getRequestBaseUrl(req), '/mcp-authorize')
const authorizePageUrl = new URL(consentPageUrl)
authorizePageUrl.searchParams.set('authRequestId', authRequestToken)
return reply.redirect(authorizePageUrl.toString())
})
}
const AuthorizeRequest = {
config: { security: securityAccess.public() },
schema: {
hide: true,
querystring: z.object({
client_id: z.string().max(64),
redirect_uri: z.string().max(2048),
response_type: z.string().max(64),
code_challenge: mcpOAuthValidation.storableText(256).refine((value) => value.length >= 43, { message: 'code_challenge is too short' }),
code_challenge_method: z.string().max(8).default('S256'),
state: mcpOAuthValidation.storableText(2048).optional(),
nonce: mcpOAuthValidation.storableText(512).optional(),
scope: mcpOAuthValidation.storableText(512).optional(),
resource: mcpOAuthValidation.storableText(2048).optional(),
}),
},
}