How to develop an MCP? Building an MCP service from scratch
This document describes a general development methodology for MCP services, covering everything from service selection and tool definition to code implementation and testing of AI assistant integration. It also compares local and remote transmission methods, enabling developers to build functional MCP services and tools from scratch.
If you want an AI assistant to directly search for information, read files, or access internal company systems, MCP (Model Context Protocol) is a common integration method. Many developers search for"how to develop an MCP,"but what they really want to know is: what components make up an MCP service, how should the tools be designed, and what steps are required to go from an empty directory to a state where it can be invoked by an AI assistant.
This article is written for developers who already know a bit of coding and want to build their first MCP service. It doesn’t cover how to use MCP services from specific brands; instead, it focuses on general methods: choosing a language and transport protocol, defining tools, writing and testing code, integrating clients, and what to watch out for during remote deployment. Follow these steps, and even if you switch to another language or framework, the underlying approach remains the same.
How to develop MCP services? One-sentence conclusion
First, choose the transmission method; then define the tools; finally, write the implementation. The MCP service is a program that sends and receives messages according to a protocol. The AI assistant (the MCP client) uses it to list available tools, invoke them, and obtain results. The table below can help you decide how to proceed:
| Your situation | Recommended practice | Reason |
|---|---|---|
| For use only with the AI assistant on my own computer | Local process mode (stdio), where the client starts the service. | No network or domain name required; simple configuration and fast debugging. |
| A team or multiple devices share the same service. | HTTP remote service, plus authentication | The service is deployed in one location, with multiple users connecting to the same address. |
| More familiar with Python or TypeScript | Use the official SDK for the corresponding language directly. | The SDK handles protocol handshakes and message formatting, reducing the underlying workload. |
| The tool needs to read large amounts of data. | Perform pagination or length limiting before returning. | If the result is too long, it will fill up the AI assistant's context window, affecting subsequent responses. |
| The tool will modify the file or data. | Write operations are designed separately, with a confirmation field added to the input parameters. | Prevent direct modifications when the AI assistant is invoked unintentionally. |
Before You Begin: First, Confirm These Points
1. A tool should do only one specific thing. Each tool corresponds to a well-defined action, such as"search internal documents by keyword,"rather than"handle all document-related tasks."The more specific the action, the easier it is for the AI assistant to determine when to invoke it.
2. Determine the language and runtime environment. The MCP official SDK is available in multiple languages; common choices are Python and TypeScript. Either language can be used for development, so choose whichever your team is familiar with and which is easiest to deploy.
3. Determine the transmission method. In local process mode, the client starts the service on the same machine and communicates via standard input/output; in remote mode, the service is provided over HTTP, requiring implementation of the corresponding transport protocol according to the current version of the MCP specification. Since the code structures for these two modes differ, finalizing this choice upfront can reduce rework.
4. Prepare a verifiable test scenario. For example, create a read-only query tool that takes keywords as input and returns a few fixed results. First, run the entire workflow using simple data, then connect it to a real data source.
5. Define clear permission boundaries. Specify in advance which directories and interfaces a service can access, and whether it can write to them, granting only the permissions necessary to complete its tasks.
Steps to Build an MCP Tool Service from Scratch
- Create a project and install the SDK. Create an empty directory, use your chosen language's package manager to install the official MCP SDK, and verify that the version matches the specification.
- Initialize the service object. Assign a name and version number to the service; these details will be displayed when clients connect, helping distinguish between multiple services.
- Register a tool. Specify the tool's name, description, and input parameter structure. Use short, English names starting with verbs, such as
search_docs. - Define input parameters using JSON Schema. For each parameter, specify its type, whether it is required, and a clear explanation of its meaning. The AI assistant uses these specifications to generate invocation parameters; the more detailed the description, the more accurate the invocation will be.
- Write a processing function. Read the input parameters, perform the actual operation, and return the result formatted as text or structured data. If an error occurs, return a result marked with an error code instead of throwing an uncaught exception directly.
- Test individually in debug mode. Before integrating with the AI assistant, use a standard MCP debugging tool or script to send list requests and invocation requests, ensuring the tool is discoverable and that input parameters and return values match expectations.
- Integrate with the AI assistant. In the configuration of an MCP-supported AI assistant (such as Claude or Cursor—clients capable of connecting to MCP), enter either the startup command for your local service or the address of the remote service. After restarting the client, verify that the tool appears in the list.
- Add security measures for remote deployment. Implement authentication for HTTP services, restrict accessible origins, set timeout limits, and log the time of each invocation along with the tool name to facilitate troubleshooting.
There are two more common pitfalls when writing this. First, in local process mode, logs must be written to the error stream; standard output should only be used for protocol messages, or the client will fail to parse them. Second, tool descriptions should be written with an AI assistant in mind—for example,"Search internal technical documentation; the query parameter is the keyword, and it returns a list of titles and summaries"—which is far more helpful than simply writing"Search tool."
Check each item after completion
| Check item | How do you see it? | What should I do if there is a problem? |
|---|---|---|
| Can the tool be listed? | Does your tool’s name appear in the client’s tool list? | Check whether the service started successfully, whether the configuration path is correct, and whether the SDK version matches. |
| Whether input parameter validation is effective | Intentionally pass in missing or incorrectly typed parameters to see if the returned result is an error. | Complete the required fields and types in the Schema, and perform another validation at the beginning of the processing function. |
| Whether the returned content is available | After the call, check the result's length, format, and whether it contains any extraneous debugging information. | Limit the number of returned entries, remove irrelevant logs, and keep field names consistent for structured content. |
| Whether exceptions are handled properly | The mock data source is unavailable; check whether the service can still respond to other requests. | Wrap the external call in a try-catch block and return an error result explaining the reason. |
| Is the write operation controlled? | When no confirmation parameter is provided, is the write operation rejected? | Add a confirmation field and state in the description that execution will only occur after confirmation. |
❓ Frequently Asked Questions (FAQ)
1. Which language does the mcp official recommend for providing external HTTP services?
The MCP official documentation provides SDKs in multiple languages and does not recommend any single one. When building HTTP-based external services, Python and TypeScript are both common choices; the key factors are whether your team can reliably maintain the chosen language and whether you have mature web frameworks and deployment experience. The specific transport implementation should follow the current version of the MCP specification.
2. What is the mcp interface? How does it differ from a standard REST interface?
The MCP interface is a protocol interface based on the JSON-RPC 2.0 message format, where clients request a list of tools or invoke tools using fixed method names. While typical REST interfaces are designed around resources and URLs, the core of MCP revolves around actions such as "listing tools" and "invoking tools," with clients automatically making these tools available for use by the AI assistant.
3. In MCP tool development experience, what is often overlooked?
Tool descriptions and parameter specifications are often hastily written. Whether the AI assistant selects a particular tool and what parameters it passes depend primarily on this text; unclear descriptions can lead to incorrect invocations or parameter errors. After writing them, you can have the AI assistant attempt to invoke the tools several times to verify that its selections match your expectations.
4. Can I develop MCP software without AI development experience?
Sure. The MCP service is essentially a standard network or local application that requires only basic programming skills, such as reading and writing files, sending HTTP requests, and handling JSON. AI-related knowledge primarily influences how to design tool descriptions, rather than being a prerequisite for developing the service.
5. Why can't the local service connect to the client?
There are three common causes: an incorrect startup command path, standard output being polluted by logs, or a mismatch between the SDK version and the specification version used by the client. First, run the service separately in the terminal and check the output; then, troubleshoot each item by comparing it with the client’s error messages.
📝 Summary
Developing an MCP service involves first defining the communication method and tool scope, then registering tools according to the protocol, defining input parameters, writing processing logic, and conducting individual testing. As long as the tool descriptions are clear, error handling is robust, and write operations include acknowledgment mechanisms, an MCP service can stably integrate with AI assistants, allowing for subsequent expansion of the number of tools and deployment methods as needed.
Open OmniDoc and integrate the document tool with your AI assistant.


