From the course: Hands-On AI: Building AI Agents with Model Context Protocol (MCP) and Agent2Agent (A2A)
Build the HR timeoff MCP server
From the course: Hands-On AI: Building AI Agents with Model Context Protocol (MCP) and Agent2Agent (A2A)
Build the HR timeoff MCP server
- [Instructor] In this video, we will build the MCP server as part of the HR time off system whose design we discussed in the previous video. Code for this chapter is in the chapter four folder. We will use SQLite to store time off information. For managing operations on the SQLite database, we have a class called TimeOffDatastore. This class is available in the file; timeoff_datastore.py. Let's first explore this file. This class has a init method that creates the SQLite database. The database is only created in memory and will not be persistent. So the tables need to be created every time the server is started. In real world scenarios, a persistent database is used, so this init method is not needed. We begin by connecting to the SQLite three database. This implicitly creates the database itself. Then we have the create tables method. This creates two tables. The employee table has one record per employee. It also tracks the total time of allowed days and the total consumed so far. The second table is the timeoff history table. This is used to log each request for employees for time offs. This method is executed as part of init. Next, there is the seed data method. This seeds some initial data into the database. It creates three employee records in the employee table with name, allowed days, and consumed days data. This is also executed as part of init. The class then has get time off balance method. Given an employee name, it fetches and returns the current time off balance for an employee. The add time off request method is used to create a new time off request for an employee based on the employee name. First, it checks if the employee has enough time off balance to file a new time off request. Then it inserts a new record in the time off history table. Finally, it updates the balance in the employee table for available time offs. There is some example usage code that can be used to test this class standalone by simply executing the Python file. Next, we move on to build the MCP server in the timeoff_db server.py file. We begin by creating a FastMCP instance for the MCP server. We initialize the time off datastore object. This is executed once when this Python file is run and the MCP server is started. It initializes the database behind the scenes. We have two tools for MCP here. The get time off balance tool calls the equivalent get time off balance method on the time off db store and returns the value. The request time off function calls the equivalent add request time off method on the time off db store and returns the results. Finally, we also have the get llm prompt function. To get the prompt for the LLM, it takes us input, the user who's using the agent and the query that the user provides for actions. We then get to run the MCP server. Here do note that we are using streamable http as the transport layer to enable HTTP. The host and port correspond to how we want the server configured for HTTP access. We can access this server using http/localhost:8000 as the URL. Now let's proceed to build the client in the next video.