Week 2 · Lesson 7 of 20

Building a Code-Based MCP Server

0% Complete

Overview

A code-based implementation provides direct control over tools, resources, and prompts. It also allows the developer to define the server in a programming language and framework that fits the surrounding application.

The supported examples span .NET/C#, Java, JavaScript or TypeScript, and Python. Official MCP SDKs handle much of the protocol work so the developer can focus on the server's functionality.

A Google Sheets and Forms server is a useful complete example. The host is a desktop AI application, and the server exposes spreadsheet capabilities.

The tool set includes listing spreadsheets, reading a sheet, writing to a sheet, and appending to a sheet. A user can ask the host to list available spreadsheets, and the host invokes the list-spreadsheets tool. The returned list gives the user a set of sheets that can be used in later requests.

The server also exposes sheet information as a resource. A user can ask for the columns of a selected spreadsheet, and the host reads the resource without modifying the sheet. The result includes the relevant sheet identifier and data.

Prompt templates add a structured workflow. An analyze-sheet-data prompt can guide the model through reading a selected sheet, analyzing its contents, and creating a dashboard. Other prompts can support report creation and form-to-sheet processing.

The server code registers each feature. A tool function is marked so the SDK knows it is an MCP tool. A resource handler is marked as a resource. Prompt-producing functions are marked as prompts, with their detailed instructions defined in the server.

The server's instructions list the tools, resources, and prompts it contains and explain how to run it. Once the server is running and connected to the host, the user can invoke the tools, read the resource, or select one of the prompt templates.

The completed spreadsheet example shows the value of combining all three features. The tool discovers and retrieves the relevant data, the resource provides read-only context, and the prompt supplies a detailed analysis procedure. The host can then produce a dashboard containing totals, scores, categories, qualitative findings, and other insights from the sheet.

A coded server can be connected to other hosts as well. The same implementation can be used by a workflow system, a custom agent, or another compatible large-language-model application.

Back to top