diff --git a/nodejs/claude/sample-agent/package.json b/nodejs/claude/sample-agent/package.json index 6d4ef65b..2f7c0fc7 100644 --- a/nodejs/claude/sample-agent/package.json +++ b/nodejs/claude/sample-agent/package.json @@ -2,6 +2,7 @@ "name": "claude-agents-sdk", "version": "1.0.0", "main": "index.js", + "type": "commonjs", "scripts": { "start": "node dist/index.js", "dev": "nodemon --watch src/*.ts --exec ts-node src/index.ts", diff --git a/nodejs/claude/sample-agent/src/agent.ts b/nodejs/claude/sample-agent/src/agent.ts index 3908f9a1..de898d64 100644 --- a/nodejs/claude/sample-agent/src/agent.ts +++ b/nodejs/claude/sample-agent/src/agent.ts @@ -11,6 +11,7 @@ import { AgentNotificationActivity } from '@microsoft/agents-a365-notifications' import { Client, getClient } from './client'; export class MyAgent extends AgentApplication { + static authHandlerName: string = 'agentic'; constructor() { super({ @@ -45,7 +46,7 @@ export class MyAgent extends AgentApplication { } try { - const client: Client = await getClient(this.authorization, turnContext); + const client: Client = await getClient(this.authorization, MyAgent.authHandlerName, turnContext); const response = await client.invokeAgentWithScope(userMessage); await turnContext.sendActivity(response); } catch (error) { diff --git a/nodejs/claude/sample-agent/src/client.ts b/nodejs/claude/sample-agent/src/client.ts index 6b68fef0..466486ea 100644 --- a/nodejs/claude/sample-agent/src/client.ts +++ b/nodejs/claude/sample-agent/src/client.ts @@ -38,12 +38,11 @@ const agentConfig = { }; -export async function getClient(authorization: Authorization, turnContext: TurnContext): Promise { +export async function getClient(authorization: Authorization, authHandlerName: string, turnContext: TurnContext): Promise { try { await toolService.addToolServersToAgent( agentConfig, - process.env.AGENTIC_USER_ID || '', - authorization, + authHandlerName, turnContext, process.env.MCP_AUTH_TOKEN || "", ); diff --git a/nodejs/claude/sample-agent/src/index.ts b/nodejs/claude/sample-agent/src/index.ts index de76eed5..2b362914 100644 --- a/nodejs/claude/sample-agent/src/index.ts +++ b/nodejs/claude/sample-agent/src/index.ts @@ -6,11 +6,11 @@ import { configDotenv } from 'dotenv'; configDotenv(); -import { AuthConfiguration, authorizeJWT, CloudAdapter, Request } from '@microsoft/agents-hosting'; +import { AuthConfiguration, authorizeJWT, CloudAdapter, loadAuthConfigFromEnv, Request } from '@microsoft/agents-hosting'; import express, { Response } from 'express' import { agentApplication } from './agent'; -const authConfig: AuthConfiguration = {}; +const authConfig: AuthConfiguration = loadAuthConfigFromEnv(); const server = express() server.use(express.json()) diff --git a/nodejs/langchain/sample-agent/package.json b/nodejs/langchain/sample-agent/package.json index 7a571d5c..9e4be76a 100644 --- a/nodejs/langchain/sample-agent/package.json +++ b/nodejs/langchain/sample-agent/package.json @@ -3,6 +3,7 @@ "version": "2025.11.6", "description": "Sample agent integrating LangChain Agents with Microsoft 365 Agents SDK and Microsoft Agent 365 SDK", "main": "src/index.ts", + "type": "commonjs", "scripts": { "preinstall": "node preinstall-local-packages.js", "start": "node dist/index.js", diff --git a/nodejs/langchain/sample-agent/src/agent.ts b/nodejs/langchain/sample-agent/src/agent.ts index e347faf7..0e37be58 100644 --- a/nodejs/langchain/sample-agent/src/agent.ts +++ b/nodejs/langchain/sample-agent/src/agent.ts @@ -8,6 +8,8 @@ import { AgentNotificationActivity } from '@microsoft/agents-a365-notifications' import { Client, getClient } from './client'; export class A365Agent extends AgentApplication { + static authHandlerName: string = 'agentic'; + constructor() { super({ startTypingTimer: true, @@ -41,7 +43,7 @@ export class A365Agent extends AgentApplication { } try { - const client: Client = await getClient(this.authorization, turnContext); + const client: Client = await getClient(this.authorization, A365Agent.authHandlerName, turnContext); const response = await client.invokeAgentWithScope(userMessage); await turnContext.sendActivity(response); } catch (error) { diff --git a/nodejs/langchain/sample-agent/src/client.ts b/nodejs/langchain/sample-agent/src/client.ts index 2de7138d..fb15d058 100644 --- a/nodejs/langchain/sample-agent/src/client.ts +++ b/nodejs/langchain/sample-agent/src/client.ts @@ -53,14 +53,13 @@ const agent = createAgent({ * const response = await client.invokeAgent("Send an email to john@example.com"); * ``` */ -export async function getClient(authorization: Authorization, turnContext: TurnContext): Promise { +export async function getClient(authorization: Authorization, authHandlerName: string, turnContext: TurnContext): Promise { // Get Mcp Tools let agentWithMcpTools = undefined; try { agentWithMcpTools = await toolService.addToolServersToAgent( agent, - '', - authorization, + authHandlerName, turnContext, process.env.BEARER_TOKEN || "", ); diff --git a/nodejs/langchain/sample-agent/src/index.ts b/nodejs/langchain/sample-agent/src/index.ts index 85bd9eff..e3db4218 100644 --- a/nodejs/langchain/sample-agent/src/index.ts +++ b/nodejs/langchain/sample-agent/src/index.ts @@ -3,11 +3,11 @@ import { configDotenv } from 'dotenv'; configDotenv(); -import { AuthConfiguration, authorizeJWT, CloudAdapter, Request } from '@microsoft/agents-hosting'; +import { AuthConfiguration, authorizeJWT, CloudAdapter, loadAuthConfigFromEnv, Request } from '@microsoft/agents-hosting'; import express, { Response } from 'express' import { agentApplication } from './agent'; -const authConfig: AuthConfiguration = {}; +const authConfig: AuthConfiguration = loadAuthConfigFromEnv(); const server = express() server.use(express.json()) diff --git a/nodejs/n8n/sample-agent/package.json b/nodejs/n8n/sample-agent/package.json index 927c290b..ac233be8 100644 --- a/nodejs/n8n/sample-agent/package.json +++ b/nodejs/n8n/sample-agent/package.json @@ -3,6 +3,7 @@ "version": "1.0.0", "description": "sample agent to integrate with n8n", "main": "src/index.ts", + "type": "commonjs", "scripts": { "start": "node ./dist/index.js", "dev": "tsx watch ./src/index.ts", diff --git a/nodejs/n8n/sample-agent/src/agent.ts b/nodejs/n8n/sample-agent/src/agent.ts index 949e154e..c77adcb2 100644 --- a/nodejs/n8n/sample-agent/src/agent.ts +++ b/nodejs/n8n/sample-agent/src/agent.ts @@ -15,7 +15,7 @@ export const agentApplication = new AgentApplication({ fileDownloaders: [downloader] }); -const n8nAgent = new N8nAgent(undefined); +const n8nAgent = new N8nAgent(); agentApplication.onActivity(ActivityTypes.Message, async (context: TurnContext, state: ApplicationTurnState) => { // Increment count state diff --git a/nodejs/n8n/sample-agent/src/mcpToolRegistrationService.ts b/nodejs/n8n/sample-agent/src/mcpToolRegistrationService.ts index 162bf7f4..e5bd435b 100644 --- a/nodejs/n8n/sample-agent/src/mcpToolRegistrationService.ts +++ b/nodejs/n8n/sample-agent/src/mcpToolRegistrationService.ts @@ -1,5 +1,5 @@ import { McpToolServerConfigurationService, McpClientTool, MCPServerConfig } from '@microsoft/agents-a365-tooling'; -import { AgenticAuthenticationService, Authorization } from '@microsoft/agents-a365-runtime'; +import { AgenticAuthenticationService, Authorization, Utility as RuntimeUtility } from '@microsoft/agents-a365-runtime'; import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'; import { Client } from '@modelcontextprotocol/sdk/client/index.js'; import { TurnContext } from '@microsoft/agents-hosting'; @@ -20,17 +20,19 @@ export class McpToolRegistrationService { private configService: McpToolServerConfigurationService = new McpToolServerConfigurationService(); async getMcpServers( - agentUserId: string, - authorization: Authorization, + authHandlerName: string, turnContext: TurnContext, authToken: string ): Promise { + const authorization = turnContext.turnState.get('authorization'); if (!authToken) { - authToken = await AgenticAuthenticationService.GetAgenticUserToken(authorization, turnContext); + authToken = await AgenticAuthenticationService.GetAgenticUserToken(authorization, authHandlerName, turnContext); } + // Get the agentic user ID from authorization configuration + const agenticAppId = RuntimeUtility.ResolveAgentIdentity(turnContext, authToken); const mcpServers: McpServer[] = []; - const servers = await this.configService.listToolServers(agentUserId, authToken); + const servers = await this.configService.listToolServers(agenticAppId, authToken); for (const server of servers) { // Compose headers if values are available diff --git a/nodejs/n8n/sample-agent/src/n8nAgent.ts b/nodejs/n8n/sample-agent/src/n8nAgent.ts index bf473976..8a73aac5 100644 --- a/nodejs/n8n/sample-agent/src/n8nAgent.ts +++ b/nodejs/n8n/sample-agent/src/n8nAgent.ts @@ -4,13 +4,12 @@ import { N8nClient } from './n8nClient'; import { McpToolRegistrationService, McpServer } from './mcpToolRegistrationService'; export class N8nAgent { + static authHandlerName: string = 'agentic'; isApplicationInstalled: boolean = false; termsAndConditionsAccepted: boolean = false; toolService: McpToolRegistrationService = new McpToolRegistrationService(); - authorization: any; - constructor(authorization: any) { - this.authorization = authorization; + constructor() { } /** @@ -181,8 +180,7 @@ export class N8nAgent { const mcpServers: McpServer[] = []; try { mcpServers.push(...await this.toolService.getMcpServers( - process.env.AGENTIC_USER_ID || '', - this.authorization, + N8nAgent.authHandlerName, turnContext, process.env.MCP_AUTH_TOKEN || "" ));