teams-mcp-server
Version:
Microsoft Teams MCP server with direct messaging support
88 lines (87 loc) • 3.51 kB
JavaScript
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js';
import { AuthManager } from './auth/authManager.js';
import { TeamsClient } from './api/teamsClient.js';
import { createAuthStatusTool } from './tools/authStatus.js';
import { createIsAuthenticatedTool } from './tools/isAuthenticated.js';
import { createAuthorizeTool } from './tools/authorize.js';
import { createListChatsStatelessTool } from './tools/listChatsStateless.js';
import { createChatStatelessTool } from './tools/createChatStateless.js';
import { createSendMessageStatelessTool } from './tools/sendMessageStateless.js';
import { createGetMessagesStatelessTool } from './tools/getMessagesStateless.js';
import * as dotenv from 'dotenv';
dotenv.config();
const CLIENT_ID = process.env.AZURE_CLIENT_ID;
const TENANT_ID = process.env.AZURE_TENANT_ID;
const CLIENT_SECRET = process.env.AZURE_CLIENT_SECRET;
const REDIRECT_URI = process.env.AZURE_REDIRECT_URI || 'http://localhost:3000/auth/callback';
const PORT = parseInt(process.env.MCP_SERVER_PORT || '3000', 10);
if (!CLIENT_ID || !TENANT_ID) {
console.error('Missing required environment variables. Please check your .env file.');
console.error('Required: AZURE_CLIENT_ID, AZURE_TENANT_ID');
process.exit(1);
}
async function main() {
const authManager = new AuthManager(CLIENT_ID, TENANT_ID, REDIRECT_URI, PORT, CLIENT_SECRET);
const teamsClient = new TeamsClient(authManager);
const server = new Server({
name: 'teams-mcp-server',
version: '1.0.0'
}, {
capabilities: {
tools: {}
}
});
// Register all tools
const tools = [
createAuthStatusTool(authManager, teamsClient), // Keep for backward compatibility
createIsAuthenticatedTool(),
createAuthorizeTool(),
createListChatsStatelessTool(),
createChatStatelessTool(),
createSendMessageStatelessTool(),
createGetMessagesStatelessTool()
];
// Handle tool calls
server.setRequestHandler(CallToolRequestSchema, async (request) => {
const tool = tools.find(t => t.name === request.params.name);
if (!tool) {
throw new Error(`Tool ${request.params.name} not found`);
}
return await tool.handler(request.params.arguments || {});
});
// Handle tool listing
server.setRequestHandler(ListToolsRequestSchema, async () => {
return {
tools: tools.map(tool => ({
name: tool.name,
description: tool.description,
inputSchema: tool.inputSchema
}))
};
});
// Error handling
process.on('SIGINT', async () => {
await server.close();
process.exit(0);
});
process.on('SIGTERM', async () => {
await server.close();
process.exit(0);
});
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
});
// Start the server
const transport = new StdioServerTransport();
await server.connect(transport);
if (process.env.DEBUG === 'true') {
console.error('Teams MCP Server started successfully');
}
}
main().catch((error) => {
console.error('Fatal error:', error);
process.exit(1);
});