Ex6 (Extra Credit) - Add a complete_task Tool to Your MCP Server:
Objective: Add a new tool, complete_task, that finds a task by name, removes it from the to-do Python list, and returns what’s left using index() and pop(), the same way you used insert() and append() to build add_task.
Description: Start by building upon the example MCP server you created, provided in the course notebook. Add a new tool, complete_task, that finds a task by name, removes it from the Python list, and returns what’s left using index() and pop(), the same way you used insert() and append() to build add_task.
- Open the main.py file you created in the Building an MCP Server for Claude Desktop course notebook page.
- Below
add_task, define a new function namedcomplete_taskthat takes one parameter, task (a string), and decorate it with@mcp.tool(), just like your other tools:
python
1@mcp.tool()
2def complete_task(task: str) -> list:
3 """Find a task by name, remove it from the to-do list, and return what's left."""- Inside the function, use index() to find where task sits in the tasks list, and store the result in a variable named position:
python
4position = tasks.index(task)- Use pop() with position to remove the task sitting at that exact spot in the list:
- Return the updated tasks list, so Claude can see what’s left.
python
5tasks.pop(position)
6return tasks- Save main.py, then completely quit and reopen Claude Desktop so it picks up the new tool.
- Start a new conversation and ask Claude, in plain English, to mark one of your tasks as done.
- For the deliverable, please upload to Canvas the main.py file you created with the new
complete_tasktool added.
Solution Example:
python
1@mcp.tool()
2def complete_task(task: str) -> list:
3 """Find a task by name, remove it from the to-do list, and return what's left."""
4 position = tasks.index(task)
5 tasks.pop(position)
6 return tasks1User: I finished "Email professor" — mark it as done.
2
3Claude: [calls complete_task with task="Email professor"]
4Nice work! Your remaining tasks are: Finish CIS240 homework, Study for math quiz.