Getting to know MCP for the first time
MCP (Model Context Protocol), is a protocol developed to standardize how applications provide context for large models.MCP provides a standard way to provide data, tools for LLM, using MCP will make it easier to build Agents or complex workflows based on LLM.
build
MCP is a CS structure where an MCP host application can link multiple MCP servers.
- MCP Host: Programs that need to get data through MCP, such as Claude Desktop, IDEs or some AI tools, etc.
- MCP Client: MCP protocol client, and MCP Server is one-to-one support.
- MCP Server: a light application that needs to expose some special capabilities through MCP. According to the MCP protocol definition, Server can provide three types of standard capabilities, Resources, Tools, Prompts, each Server can simultaneously provide three types of capabilities or one of them.
- Resources: resources, similar to file data reading, can be file resources or the content returned by the API response.
- Tools: Tools, third-party services, function functions, through which you can control which functions can be called by LLM.
- Prompts: Prompts, pre-defined templates for the user to accomplish specific tasks.
- Local Data Resources: Local files, databases, services, etc. that MCP Server can securely access.
- Remote Service: An external system to which the MCP Server can connect through a network (e.g., API).
Schematic of MCP's services:
MCP Servers provide some list of functions to the Host Application via the MCP Protocol (e.g., provide a list of tools), and then the Host Application formats this list into a format that LLM can read and understand. Host Application can use this feature list to send some requests to LLM that need to be processed by the big model (this is the prompt), and LLM will return a json string of tool_calls based on this prompt. When Host Applicaiton receives this tool_calls, it will call the corresponding MCP server tool to return the corresponding results.
Using MCP with Claude Desktop
With the help of the Claude desktop client, you definitely need to install one first, and this installation is skipped. We configure a file system MCP Server.
Then select Edit Config under Developer,
show (a ticket)claude_desktop_config.json
To complete the MCP Server configuration of the file system. (Configuration username needs to be replaced with the username of your own computer, but also need to locally install the node.js environment.)
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": [
"-y".
"@modelcontextprotocol/server-filesystem",
"/Users/username/Desktop",
"/Users/username/Downloads"
]
}
}
}
Once the configuration is complete, restart the Cluade client. It will be ready to try it out.
Click on the hammer icon to see the tools provided by MCP Server.
You can try it out by typing the following prompt
Can you take all the images on my desktop and move them to a new folder called "Images"?
There are also several reminders of permissions when LLM's feedback and file system MCP server are executed, remember to allow them.
Then you can see LLM, MCPServer start working.
Getting Started Tutorial for MCP Server Development (python and pip)
Simple mcp server using the python technology stack
Requires installation
For MCP server, you need to use python-sdk, python needs to be 3.10, install as follows
pip install mcp
PS: MCP officially uses the uv package management tool, I usually use pip more, so the text is mainly pip. Because some of the dependencies of the mcp package version is not the latest, so it is best to get a clean environment. Developing an MCP Server requires a debugging client, MCP inspector provides this function
npx @modelcontextprotocol/inspector
Where command we should python if we are using Server written in python.
is an optional parameter. After startup
Develop a demo MCP Server
MCP Server: a light application that needs to expose some special capabilities through MCP. According to the MCP protocol definition, Server can provide three types of standard capabilities, Resources, Tools, Prompts, each Server can simultaneously provide three types of capabilities or one of them.
- Resources: resources, similar to file data reading, can be file resources or the content returned by the API response.
- Tools: Tools, third-party services, function functions, through which you can control which functions can be called by LLM.
- Prompts: Prompts, pre-defined templates for the user to accomplish specific tasks.
Here is a demonstration of the three types of capabilities, using python-sdk.
Prompts
Let's start with the prompt and go through thehandle_list_promopts
Listing of available cue word templates.handle_get_prompt
is to get a specific prompt template based on the name.
@server.list_prompts() async def handle_list_prompts() -> list[types.] """ Prompt template definition """ return [ types.Prompt( name="example-prompt", description="An example prompt template", arguments=[ types.PromptArgument( name="arg1", description="Example argument", required=True ) ] ) ] @server.get_prompt() async def handle_get_prompt( name: str, arguments: dict[str, str] | None arguments: dict[str, str] | None ) -> types.GetPromptResult. """ Prompt template handling """ if name ! = "example-prompt": raise ValueError(f "Unknown prompt: {name}") raise ValueError(f "Unknown prompt: {name}") return types.GetPromptResult( description="Example prompt", messages=[ types.PromptMessage( role="user", content=types.TextContent( content=types.TextContent( content=types.TextContent( text="Example prompt text" ) ) ] )
Resources
Code for resource management functionslist_resources
Lists the available resources, returning a list of resources.read_resource
SAMPLE_RESOURCES is a demo built for testing.
@server.list_resources() async def list_resources() -> list[types.] """ Resource definition """ test='test' return [ types.Resource( uri=AnyUrl(f "file:///{test}.txt"), name=test, description=f "A sample text resource named {test}", mimeType="text/plain", ) ) # for name in SAMPLE_RESOURCES.keys() ] SAMPLE_RESOURCES={'test':'this demo is a mcp server!'} @server.read_resource() async def read_resource(uri: AnyUrl) -> str | bytes. assert uri.path is not None print(uri.path) name = uri.path.replace(".txt", "").lstrip("/") # print(name) if name not in SAMPLE_RESOURCES: raise ValueError(f"/"). raise ValueError(f "Unknown resource: {uri}") return SAMPLE_RESOURCES[name]
Tools
tool definitions and invocations.handle_list_tools
Define available tools and validate tool parameters using JSON Schema.handle_call_tool
Handles tool calls, performing the appropriate action based on the tool name and parameters. @server.list_tools() async def handle_list_tools() -> list[types.Tool]: """ Tool definition. Each tool specifies its parameters using JSON Schema validation. """ return [ types.Tool( name="demo-tool", description="Get data tool for a param", inputSchema={ "type": "object", "properties": { "param": { "type ": "string", "description": "url", }, }, "required": ["param"], }, ) ] @server.call_tool() async def handle_call_tool( name: str, arguments: dict | None ) -> list[Any]: logging.info(name) """ handle_call_tool """ if not arguments: raise ValueError("Missing arguments") if name == "demo-tool": param = arguments.get("param") if not param: raise ValueError("Missing state parameter") param = param.upper() return [ types.TextContent( type="text", text=f "text:{par","","","","","") text=f "text:{param}" ) ] else: raise ValueError(f "Unknown tool: {name}")
inspector
If you have written an MCP Server as follows, you can start the MCP inspector for debugging, and in the directory where the Server code is located, enter
npx @modelcontextprotocal/inspector
After startup, get the following screenshot according to the figure, visit http://localhost:5273, according to what I said earlier, select STDIO for the transport type, enter python for the command, enter server.py for the Arguments (the above demo code is saved to the server.py file), click connect. In the diagram, you can enter the corresponding Server service call by each type, and click List Resource in Resources to list all the resources, and click the corresponding resource to see the content of the specific resource.
This will allow us to interact with the MCP Server we have developed.
How to configure into Claude desktop
Configure this MCP Server into Claude by following the command configuration you just did in inspector. Click on Claude desktop's setting, select developer's tab and then click edit config to jump to claude_desktop_config.json.
I currently have two MCP Servers installed with the following configurations, one for file organizing and one for playwright (to install playwright's MCP Server via npx.
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": [
"-y".
"@modelcontextprotocol/server-filesystem",
"/Users/crisschan/Desktop",
"/Users/crisschan/Downloads"
]
}, }
"playwright": {
"command": "npx", "args": ["args" - ["npx" ], "playwright": {
"args": ["-y".
"@executeautomation/playwright-mcp-server"]
}
}
}
Configure our own demo service in this format, then save and restart Claude desktop.
{
"mcpServers": {
,
"demo": {
"command":"/opt/anaconda3/bin/python3", ,
"args": ["/Users/workspace/pyspace/try_mcp/server.py"]
}
}
}
Where in the configuration, the command must be the absolute address of the corresponding version of python, and the same goes for the location of the server.py code in args, to use the absolute address.