@microsoft/agents-a365-tooling
Version:
Agent 365 Tooling SDK for AI agents built with TypeScript/Node.js
379 lines • 19.4 kB
JavaScript
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import fs from 'fs';
import path from 'path';
import axios from 'axios';
import { OperationResult, OperationError, AgenticAuthenticationService, Utility as RuntimeUtility } from '@microsoft/agents-a365-runtime';
import { Utility } from './Utility';
import { defaultToolingConfigurationProvider, resolveTokenScopeForServer } from './configuration';
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
/**
* Service responsible for discovering and normalizing MCP (Model Context Protocol)
* tool servers and producing configuration objects consumable by the Claude SDK.
*/
export class McpToolServerConfigurationService {
/**
* Construct a McpToolServerConfigurationService.
* @param configProvider Optional configuration provider. Defaults to defaultToolingConfigurationProvider if not specified.
*/
constructor(configProvider) {
this.logger = console;
this.configProvider = configProvider ?? defaultToolingConfigurationProvider;
}
async listToolServers(agenticAppIdOrTurnContext, authTokenOrAuthorization, optionsOrAuthHandlerName, authTokenOrOptions, options) {
// Detect which signature is being used based on the type of the first parameter
if (typeof agenticAppIdOrTurnContext === 'string') {
// LEGACY PATH: listToolServers(agenticAppId, authToken, options?)
const agenticAppId = agenticAppIdOrTurnContext;
// Runtime validation for legacy signature parameters
if (typeof authTokenOrAuthorization !== 'string') {
throw new Error('authToken must be a string when using the legacy listToolServers(agenticAppId, authToken) signature');
}
const authToken = authTokenOrAuthorization;
const toolOptions = optionsOrAuthHandlerName;
const servers = await (this.isDevScenario()
? this.getMCPServerConfigsFromManifest()
: this.getMCPServerConfigsFromToolingGateway(agenticAppId, authToken, undefined, toolOptions));
// Apply per-audience tokens on the legacy path too, using the same structural path as the
// new overload so V2 servers are never silently missing an Authorization header.
// Dev: reads from BEARER_TOKEN_<NAME> / BEARER_TOKEN env vars, supports V1 and V2.
// Prod: uses the shared authToken for V1 servers; throws for V2 servers (OBO requires
// Authorization and authHandlerName — use the TurnContext-based overload instead).
const acquire = this.isDevScenario()
? this.createDevTokenAcquirer()
: this.createLegacyProdTokenAcquirer(authToken);
return await this.attachPerAudienceTokens(servers, acquire);
}
else {
// NEW PATH: listToolServers(turnContext, authorization, authHandlerName, authToken?, options?)
const turnContext = agenticAppIdOrTurnContext;
// Runtime validation for new signature parameters
if (typeof authTokenOrAuthorization === 'string') {
throw new Error('authorization must be an Authorization object when using the new listToolServers(turnContext, authorization, authHandlerName) signature');
}
if (typeof optionsOrAuthHandlerName !== 'string') {
throw new Error('authHandlerName must be a string when using the new listToolServers(turnContext, authorization, authHandlerName) signature');
}
const authorization = authTokenOrAuthorization;
const authHandlerName = optionsOrAuthHandlerName;
let authToken = authTokenOrOptions;
const toolOptions = options;
// Auto-generate token if not provided
if (!authToken) {
const scopes = [this.configProvider.getConfiguration().mcpPlatformAuthenticationScope];
authToken = await AgenticAuthenticationService.GetAgenticUserToken(authorization, authHandlerName, turnContext, scopes);
if (!authToken) {
throw new Error('Failed to obtain authentication token from token exchange');
}
}
// Note: Token validation (format/expiration) is performed inside getMCPServerConfigsFromToolingGateway()
// to avoid duplicate validation (it's also called by the legacy path)
// Resolve agenticAppId from TurnContext
const agenticAppId = RuntimeUtility.ResolveAgentIdentity(turnContext, authToken);
// Discover servers: manifest in dev, gateway in prod
const servers = await (this.isDevScenario()
? this.getMCPServerConfigsFromManifest()
: this.getMCPServerConfigsFromToolingGateway(agenticAppId, authToken, turnContext, toolOptions));
// Acquire and attach per-server tokens via the same structural path in both envs.
// Token source differs: env vars in dev, OBO in prod.
const acquire = this.isDevScenario()
? this.createDevTokenAcquirer()
: this.createOboTokenAcquirer(authorization, authHandlerName, turnContext);
return await this.attachPerAudienceTokens(servers, acquire);
}
}
/**
* Acquire one token per unique audience across the provided server list and attach
* the correct `Authorization: Bearer` header to each server's headers.
* V1 servers (no `audience` field, or ATG AppId) all share the same token (one exchange).
* V2 servers each get a token scoped to their own audience GUID.
* Token acquisition is delegated to `acquire`, enabling different strategies in dev
* (env vars via createDevTokenAcquirer) and prod (OBO via createOboTokenAcquirer)
* while keeping scope resolution, deduplication, and header attachment identical.
*/
async attachPerAudienceTokens(servers, acquire) {
// Fetch once so scope resolution and the legacy-path guard use the same value.
const sharedScope = this.configProvider.getConfiguration().mcpPlatformAuthenticationScope;
const tokenCache = new Map(); // scope → token (null = no token available)
const result = [];
for (const server of servers) {
const scope = resolveTokenScopeForServer(server, sharedScope);
if (!tokenCache.has(scope)) {
tokenCache.set(scope, await acquire(server, scope));
}
const token = tokenCache.get(scope);
result.push(token
? { ...server, headers: { ...server.headers, Authorization: `Bearer ${token}` } }
: server // no token available — dev no-op; prod acquirer would have thrown already
);
}
return result;
}
/**
* Returns a TokenAcquirer that resolves tokens from environment variables (local dev only).
* Resolution order per server:
* 1. BEARER_TOKEN_<MCPSERVERNAME_UPPER> — per-server token (effective for V2 unique audiences)
* 2. BEARER_TOKEN — shared fallback (V1 servers share one token)
* Returns null when neither variable is set; no Authorization header is attached.
* Emits a warning when a V2 server (distinct audience) falls back to the shared BEARER_TOKEN,
* because that token is scoped to the shared ATG audience and will cause a 401 at the server.
*/
createDevTokenAcquirer() {
const sharedScope = this.configProvider.getConfiguration().mcpPlatformAuthenticationScope;
return (server, scope) => {
const serverName = server.mcpServerName ?? '';
const config = this.configProvider.getConfiguration();
const token = config.getBearerTokenForServer(serverName);
if (token && !config.hasPerServerBearerToken(serverName) && scope !== sharedScope) {
this.logger.warn(`Dev: MCP server '${serverName}' requires scope '${scope}' but only BEARER_TOKEN is set. ` +
`The shared token is scoped to a different audience and will likely cause a 401. ` +
`Set BEARER_TOKEN_${serverName.toUpperCase()} to a token acquired for the correct audience.`);
}
return Promise.resolve(token ?? null);
};
}
/**
* Returns a TokenAcquirer for the deprecated legacy (agenticAppId, authToken) overload in prod.
* V1 servers (ATG shared scope) receive the caller-supplied authToken directly.
* V2 servers (per-audience scope) throw immediately — OBO exchange requires Authorization and
* authHandlerName which the legacy signature does not provide; callers must migrate to the
* TurnContext-based overload.
*/
createLegacyProdTokenAcquirer(authToken) {
const sharedScope = this.configProvider.getConfiguration().mcpPlatformAuthenticationScope;
return (server, scope) => {
if (scope !== sharedScope) {
throw new Error(`MCP server '${server.mcpServerName}' requires a per-audience token (scope: '${scope}'). ` +
`Per-audience token exchange is not supported by the deprecated listToolServers(agenticAppId, authToken) overload. ` +
`Migrate to listToolServers(turnContext, authorization, authHandlerName) instead.`);
}
return Promise.resolve(authToken);
};
}
/**
* Returns a TokenAcquirer that performs OBO token exchange via AgenticAuthenticationService.
* Throws if the exchange returns null so callers receive an explicit error rather than a
* silently missing Authorization header.
*/
createOboTokenAcquirer(authorization, authHandlerName, turnContext) {
return async (server, scope) => {
const token = await AgenticAuthenticationService.GetAgenticUserToken(authorization, authHandlerName, turnContext, [scope]);
if (!token) {
throw new Error(`Failed to obtain token for MCP server '${server.mcpServerName}' (scope: ${scope})`);
}
return token;
};
}
/**
* Connect to the MCP server and return tools with names prefixed by the server name.
* Throws if the server URL is missing or the client fails to list tools.
*/
async getMcpClientTools(mcpServerName, mcpServerConfig) {
if (!mcpServerConfig) {
throw new Error('Invalid MCP Server Configuration');
}
if (!mcpServerConfig.url) {
throw new Error('MCP Server URL cannot be null or empty');
}
const transport = new StreamableHTTPClientTransport(new URL(mcpServerConfig.url), {
requestInit: {
headers: mcpServerConfig.headers
}
});
const mcpClient = new Client({
name: mcpServerName + ' Client',
version: '1.0',
});
await mcpClient.connect(transport);
const toolsObj = await mcpClient.listTools();
await mcpClient.close();
return toolsObj.tools;
}
async sendChatHistory(turnContext, chatHistoryMessages, options) {
if (!turnContext) {
throw new Error('turnContext is required');
}
if (!chatHistoryMessages) {
throw new Error('chatHistoryMessages is required');
}
// Extract required information from turn context
const conversationId = turnContext.activity?.conversation?.id;
if (!conversationId) {
throw new Error('Conversation ID is required but not found in turn context');
}
const messageId = turnContext.activity?.id;
if (!messageId) {
throw new Error('Message ID is required but not found in turn context');
}
const userMessage = turnContext.activity?.text;
if (!userMessage) {
throw new Error('User message is required but not found in turn context');
}
// Get the endpoint URL
const endpoint = this.getChatHistoryEndpoint();
this.logger.info(`Sending chat history to endpoint: ${endpoint}`);
// Create the request payload
const request = {
conversationId,
messageId,
userMessage,
chatHistory: chatHistoryMessages
};
try {
const headers = Utility.GetToolRequestHeaders(undefined, turnContext, options);
await axios.post(endpoint, request, {
headers: {
...headers,
'Content-Type': 'application/json'
},
timeout: 10000 // 10 seconds timeout
});
this.logger.info('Successfully sent chat history to MCP platform');
return OperationResult.success;
}
catch (err) {
const error = err;
if (axios.isAxiosError(err)) {
if (err.code === 'ECONNABORTED' || err.code === 'ETIMEDOUT') {
this.logger.error(`Request timeout sending chat history to '${endpoint}': ${error.message}`);
}
else {
this.logger.error(`HTTP error sending chat history to '${endpoint}': ${error.message}`);
}
}
else {
this.logger.error(`Failed to send chat history to '${endpoint}': ${error.message}`);
}
return OperationResult.failed(new OperationError(error));
}
}
/**
* Query the tooling gateway for MCP servers for the specified agent and normalize each entry's mcpServerUniqueName into a full URL using Utility.BuildMcpServerUrl.
* Throws an error if the gateway call fails.
*
* @param agenticAppId The agentic app id used by the tooling gateway to scope results.
* @param authToken Optional Bearer token to include in the Authorization header when calling the gateway.
* @param turnContext Optional TurnContext for extracting agent blueprint ID for request headers.
* @param options Optional tool options when calling the gateway.
* @throws Error when the gateway call fails or returns an unexpected payload.
*/
async getMCPServerConfigsFromToolingGateway(agenticAppId, authToken, turnContext, options) {
// Validate the authentication token
Utility.ValidateAuthToken(authToken);
const configEndpoint = this.getToolingGatewayUrl(agenticAppId);
try {
const response = await axios.get(configEndpoint, {
headers: Utility.GetToolRequestHeaders(authToken, turnContext, options),
timeout: 10000 // 10 seconds timeout
});
const rawServers = response.data?.mcpServers ?? [];
return rawServers.map(s => ({
mcpServerName: s.mcpServerName,
url: s.url,
headers: s.headers,
audience: s.audience,
scope: s.scope,
publisher: s.publisher,
}));
}
catch (err) {
const error = err;
throw new Error(`Failed to read MCP servers from endpoint: ${error.code || 'UNKNOWN'} ${error.message || 'Unknown error'}`);
}
}
/**
* Read MCP servers from a local ToolingManifest.json file (development only).
* Searches process.cwd() and process.argv[1] for the manifest file.
*
* Reads MCP server configurations from ToolingManifest.json in the application's content root.
* The file should be located at: [ProjectRoot]/ToolingManifest.json
*
* Example ToolingManifest.json:
* {
* "mcpServers": [
* {
* "mcpServerName": "mailMCPServerConfig",
* "mcpServerUniqueName": "mcp_MailTools"
* },
* {
* "mcpServerName": "sharePointMCPServerConfig",
* "mcpServerUniqueName": "mcp_SharePointTools"
* }
* ]
* }
*
* Each server entry can optionally include a "url" field to specify a custom MCP server URL.
* If the "url" field is not provided, the URL will be automatically constructed using the server name.
* The server name is determined by using "mcpServerName" if present, otherwise "mcpServerUniqueName".
*/
async getMCPServerConfigsFromManifest() {
let manifestPath = path.join(process.cwd(), 'ToolingManifest.json');
if (!fs.existsSync(manifestPath)) {
this.logger.warn(`ToolingManifest.json not found at ${manifestPath}, checking argv[1] location.`);
manifestPath = path.join(path.dirname(process.argv[1] || ''), 'ToolingManifest.json');
}
if (!fs.existsSync(manifestPath)) {
this.logger.warn(`ToolingManifest.json not found at ${manifestPath}`);
return [];
}
try {
const jsonContent = fs.readFileSync(manifestPath, 'utf-8');
const manifestData = JSON.parse(jsonContent);
const mcpServers = manifestData.mcpServers || [];
return mcpServers.map((s) => {
// Use mcpServerName if available, otherwise fall back to mcpServerUniqueName
const serverName = s.mcpServerName || s.mcpServerUniqueName;
if (!serverName) {
throw new Error('Either mcpServerName or mcpServerUniqueName must be provided in manifest entry');
}
return {
mcpServerName: serverName,
url: s.url || this.buildMcpServerUrl(serverName),
headers: s.headers,
audience: s.audience,
scope: s.scope,
publisher: s.publisher,
};
});
}
catch (err) {
const error = err;
this.logger.error(`Error reading or parsing ToolingManifest.json: ${error.message || 'Unknown error'}`);
return [];
}
}
/**
* Detect if the process is running in a development scenario based on configuration.
*
* @returns {boolean} True when running in a development environment (NODE_ENV=Development).
*/
isDevScenario() {
return this.configProvider.getConfiguration().useToolingManifest;
}
/**
* Gets the base URL for MCP platform from configuration.
*/
getMcpPlatformBaseUrl() {
return this.configProvider.getConfiguration().mcpPlatformEndpoint;
}
/**
* Construct the tooling gateway URL for a given agent identity.
*/
getToolingGatewayUrl(agenticAppId) {
return `${this.getMcpPlatformBaseUrl()}/agents/v2/${agenticAppId}/mcpServers`;
}
/**
* Build the full URL for accessing a specific MCP server.
*/
buildMcpServerUrl(serverName) {
return `${this.getMcpPlatformBaseUrl()}/agents/servers/${serverName}/`;
}
/**
* Constructs the endpoint URL for sending chat history.
*/
getChatHistoryEndpoint() {
return `${this.getMcpPlatformBaseUrl()}/agents/real-time-threat-protection/chat-message`;
}
}
//# sourceMappingURL=McpToolServerConfigurationService.js.map