Location>code7788 >text

Understand the difference between MCP protocol and Function Call in one article

Popularity:828 ℃/2025-03-23 09:03:48

1. Preface

Hello everyone, I am Brother Six!

Today, let’s talk about two concepts in programming that sound a bit complicated - MCP protocol and function call. In fact, in plain words, they are two different "communication methods", just like there are different ways of communicating between people. Below I will use life examples and Python code to explain the differences to you.

2. What is Function Call

1. Life examples

Let’s talk about Function Call first. Function Call is like you go to a restaurant to order food. You walk into the restaurant and say to the waiter, "Give me a braised pork." After hearing your words, the waiter goes to the kitchen to tell the chef your needs. The chef cooks according to this need, and after it is ready, the waiter will serve you the dishes. In this process, the action of your ordering is equivalent to a Function Call. You are the caller, the waiter is the medium for delivering needs, and the chef is the person who actually performs the task.

2. Python code examples

def make_red_cooked_pork():
     return "A delicious braised pork"

 # Call function
 dish = make_red_cooked_pork()
 print(dish)

In this code,make_red_cooked_porkIt’s that “chef” that defines how to make braised pork. anddish = make_red_cooked_pork()This line of code is your "order" action, which is a Function Call. After being called, the function will return the result and finally print it out.

3. Features of Function Call

Function Call has a feature, that is, it is synchronized. What does it mean? That is to say, after you call a function, the program will wait for the function to be executed and return the result to you before continuing to execute the subsequent code. Just like after ordering in a restaurant, you have to wait for the dishes to be prepared before you can continue to do something else.

3. What is the MCP protocol?

1. Life examples

The MCP protocol is like you shop online. You place an order on the shopping platform and buy a piece of clothing. After placing an order, you don’t have to wait for the clothes to be delivered to you before you can do anything else. You can do whatever you should do, go to work, and watch TV series. After the clothes are ready, the shopping platform will arrange express delivery to you. When the express delivery arrives near your home, it will send you a message notifying you of picking up the item. In this process, your order is equivalent to sending a request through the MCP protocol. The shopping platform and express delivery are the systems that handle the request, and they will give you feedback on the results at the right time.

2. Python code examples

import asyncio

 async def prepare_clothes():
     print("Start prepare clothes...")
     await (3) # It takes 3 seconds to prepare clothes in simulation
     print("Clothes are ready, shipment starts...")
     return "A beautiful dress"

 async def order_clothes():
     task = asyncio.create_task(prepare_clothes())
     print("The order was successfully placed, you can do something else.")
     result = await task
     print("Clothes are here:", result)

 # Run asynchronous functions
 (order_clothes())

In this code,prepare_clothesFunctions are like shopping platforms and express delivery, responsible for handling your request to buy clothes.order_clothesIn the function,asyncio.create_task(prepare_clothes())This line of code is equivalent to you placing an order, and the program will not waitprepare_clothesAfter the function is executed, it will continue to execute the subsequent code and print out "The order is successfully placed, you can do something else." waitprepare_clothesAfter the function is executed, passawait taskGet the results.

3. Characteristics of MCP protocol

The MCP protocol is asynchronous. That is to say, after you send a request, the program will not stop and wait for the result, but will continue to execute other code. When the result comes out, come back to deal with this result. Just like after placing an order online, you don’t have to wait for your clothes to arrive, and you can do other things.

4. Summary of the difference between MCP protocol and Function Call

1. Synchronous and asynchronous

Function Call is synchronous. After calling the function, the program will wait for the function to execute and return the result before continuing to execute the subsequent code; while the MCP protocol is asynchronous. After sending the request, the program will not wait for the result and will continue to execute other codes and wait until the result comes out before processing.

2. Execution method

Function Call is like when you order food in a restaurant, you have to wait until the food is ready before you can continue to do something else; the MCP protocol is like online shopping, you can do other things after placing an order and then deal with things.

3. Application scenarios

Function Call is suitable for scenarios where the result needs to be obtained immediately and subsequent code depends on this result; the MCP protocol is suitable for scenarios where the processing time is long, the result does not need to be obtained immediately, and the program does not want to block, such as network requests, file reading and writing, etc.

5. Ending

Through the above life examples and Python code, I believe everyone has a clearer understanding of the difference between MCP protocol and Function Call. In fact, many concepts in programming can be linked to things in life, so it will be much easier to understand. I hope everyone can go smoothly on the road of AI!

Okay, let’s all for this introduction first. If you think it’s useful to you, please mark me.

Your likes, reading, leaving messages and sharing are my biggest motivation for continuous updates! (Please)