From the course: Hands-On AI: Building AI Agents with Model Context Protocol (MCP) and Agent2Agent (A2A)
Build the code-of-conduct MCP server
From the course: Hands-On AI: Building AI Agents with Model Context Protocol (MCP) and Agent2Agent (A2A)
Build the code-of-conduct MCP server
- [Instructor] In this video, we will build the code of conduct MCP server. Code for all examples in this course are available in the GitHub link provided with the course. I will use code spaces to run the code. For this, we can start code spaces on the main branch. You can click code and then say create code space on main. This will start a new environment and install all the dependency packages. This will take a few minutes to execute. The setup is now complete, code for this chapter is available in the folder chapter two. The code of conduct server is available in the code of conduct server dot py files. The PDF file to be used is also in the same directory. Let's now explore this code file. We will be using FastMCP SDK to build the MCP server. We need to first load the dot ENV environment, since it is used by the underlying SDK, we then create an instance of FastMCP. We specify a name for the MCP server, the HR underscore CCO underscore MCP variable is important, and it'll be used in the decorators later. Then we proceed to set up a MCP resource. This process is similar for each resource, irrespective of the number of resources set up in a single server, we first create a URA for the PDF file, specifying the full path for the file. Then we create a decorator for the MCP resource. This is done using the FastMCP instance we created earlier. We specify the URA of the file to load. Then we provide a name, description, and mine type for the resource. Information in the decorator will be provided to clients to understand the resource and the type of data it provides. So it's good practice to be elaborate in the description. The mime type identifies the type of content returned. Beyond text, it can be a URA, audio, or streaming video. Next, we define a function that will actually return the resource. This corresponds to the decorator above. The implementation of this function is custom and specific to the resource and use case. A developer is free to implement any logic they want to, to provide the right information back. This function can in fact read from a database or another web service if needed. This function can actually take inputs if needed from the client and adapt the output accordingly. In this example, there are no inputs. The function returns a string, which in this case is the contents of the PDF file. Inside the function, we read the contents of the PDF file and create a text string with its contents. We will finally return that text. The MCP server itself need to be run when this file is executed. For this, we call the run method in HR COC MCP, we specify transport as STD io. The client will actually run this file, fetch the resource, and then shut down the running instance.