All articles
Article 3 min read

Building a OneNote MCP Server with TypeScript: Insights on Microsoft Graph Authentication

Learn how to create a OneNote MCP server using TypeScript while delving into Microsoft Graph authentication techniques and best practices.

Introduction

Creating a Microsoft Cloud-based application can be a rewarding endeavor but also comes with challenges, especially when dealing with authentication and integration. A recent project involving a OneNote MCP (Microsoft Cloud Platform) server built in TypeScript provided significant insights into leveraging Microsoft Graph for authentication. In this article, we will explore the essential steps to build such a server, share some lessons learned, and provide a better understanding of Microsoft Graph authentication.

Understanding MCP and Microsoft Graph

The Microsoft Cloud Platform (MCP) is a versatile set of cloud services that allows developers to harness the power of Microsoft technologies in their applications. OneNote, as a part of this ecosystem, plays an essential role in capturing and managing information.

What is Microsoft Graph?

Microsoft Graph serves as a unified API endpoint that enables developers to access a variety of Microsoft services such as Azure Active Directory, OneDrive, Outlook, and many more. It acts as an intermediary, allowing seamless access to various resources while managing authentication and authorization effortlessly.

The Role of Authentication

Authentication is critical in ensuring that only authorized users can access or manipulate their OneNote data. Microsoft Graph provides OAuth 2.0 authorization, which enables developers to authenticate users efficiently. In our project, this process was crucial for building a secure server that adheres to best practices.

Setting Up the TypeScript Environment

Starting with TypeScript allows you to build applications with strong typing, enhancing readability and maintainability. To set up a OneNote MCP server in TypeScript, follow these steps:

1.

Initialize Your Project: Start by creating a new directory for your project and initializing it using npm:

bash
   mkdir onenote-mcp-server
   cd onenote-mcp-server
   npm init -y
2.

Install Required Packages: You will need some packages, including express for your web server and @microsoft/microsoft-graph-client for accessing the Graph API:

bash
   npm install express @microsoft/microsoft-graph-client @azure/msal-node
3.

Set Up Type Definitions: Make sure to install TypeScript and the necessary type definitions:

bash
   npm install typescript --save-dev
   npm install @types/express --save-dev
4.

Create a Basic Server: Create a simple Express server in server.ts:

typescript
   import express from 'express';

   const app = express();
   const PORT = process.env.PORT || 3000;

   app.get('/', (req, res) => {
       res.send('Welcome to the OneNote MCP Server!');
   });

   app.listen(PORT, () => {
       console.log(`Server is running on http://localhost:${PORT}`);
   });
5.

Run the Server: Using TypeScript, compile and run your server:

bash
   tsc server.ts && node server.js

Integrating Microsoft Graph Authentication

Successfully implementing Microsoft Graph’s authentication is a critical step. Using the Microsoft Authentication Library (MSAL), you can authenticate users and obtain access tokens for Graph API.

Steps to Implement Authentication

1.

Register Your Application: Head to the Azure portal, create a new app registration, and set up Redirect URIs. Note down your Client ID and Client Secret.

2.

Set Up MSAL: Configure MSAL in your project:

typescript
   import { ConfidentialClientApplication } from '@azure/msal-node';

   const clientApp = new ConfidentialClientApplication({
       auth: {
           clientId: 'YOUR_CLIENT_ID',
           clientSecret: 'YOUR_CLIENT_SECRET',
           authority: 'https://login.microsoftonline.com/YOUR_TENANT_ID',
       },
   });
3.

Get Access Token: Implement a method to acquire tokens:

typescript
   async function getAccessToken() {
       const result = await clientApp.acquireTokenByClientCredential({
           scopes: ['https://graph.microsoft.com/.default'],
       });
       return result.accessToken;
   }
4.

Use the Access Token to Call the API: You can now use the token to interact with the OneNote API:

typescript
   async function getOneNoteContent(token: string) {
       const client = Client.init({
           authProvider: (done) => {
               done(null, token); // pass token to the auth provider
           },
       });

       const response = await client.api('/me/onenote/notebooks').get();
       return response;
   }

Key Takeaways

1.

TypeScript Offers Clarity: Using TypeScript provides a more structured approach to coding, reducing bugs.

2.

**Power of