Integrate a Web API using MCP to install on your Claude desktop

Here I discuss how I created an MCP Plugin that connects with the US Gov Spending API and gets the accurate details.

If you don’t know what MCP is, I encourage you to review my previous blog for MCP here.

In this post, I demonstrate how to add a WebAPI to your Claude desktop and use that in your chatbot.

Code is here.

Step by step on how to install a Python API plugin using MCP into your Claude desktop

1. Fast MCP and UV pre requisites

I use FastMCP to create an ergonomic interface using MCP via API. UV is used for the Virtual environment setup. Read through the post here for more details on how to set up the uv environment. An overview of how to use MCP in the Python SDK is here. Install the Claude desktop for Windows.

2. Expose the API function calls that are needed as MCP tools

I create a server folder and expose the API’s. Notice below that I keep the correct URL through which the server code calls the USGovSpending and waits for the response in an async manner.

@mcp.tool()
async def get_spending_all_state()->str:
    """Get US Spending data for state based on FIPS code.
    """
    url = f"{NWS_API_BASE}/api/v2/recipient/state/"
    print(url)
    data = await make_all_awards_request(url)
    print(data)
    result = "Invalid argument"
    
    if not data :
        return "Unable to fetch adata"
    return data

Similarly, in the following GitHub code, I exposed a few more API’s. View the code at this link.

3. Run and test on your standalone application

Using the command,

uv run mcp dev usgovspending.py

The command runs the python file and opens a local host link to the browser that you can click and select the tools that you want to run.

When click on connect and Tools, I can see all the API’s that I exposed in the usgovspending.py file.
It goes ahead and gets the top-awarded agency to be the Department of Defense. Similarly, one can test other API results as well.

If everything looks good. Lets go ahead and install this on to the Claude desktop.

4. Integrate the API to Claude desktop

Use the command

uv install usgovspending.py

This should install the API integrate it with your Claude desktop. This updates the config.json file, which you can look up in your desktop claude->file-> settings->developer.

The json config file looks similar to this

{
  "mcpServers": {
    "weather": {
      "command": "C:\\Users\\****\\.local\\bin\\uv.EXE",
      "args": [
        "run",
        "--with",
        "mcp[cli]",
        "mcp",
        "run",
        "C:\\MCP\\1\\server\\weather.py"
      ]
    },
    "USA Spending": {
      "command": "C:\\Users\\***\\.local\\bin\\uv.EXE",
      "args": [
        "run",
        "--with",
        "mcp[cli]",
        "mcp",
        "run",
        "C:\\MCP\\2\\server\\usgovspending.py"
      ]
    }
  }
}

You can view the extension integrated with your chatbot as follows

5. Run this integrated with your chatbot

When I asked, Where does USA spend second largest on? It goes ahead and calls the API that I integrated and gets me some answers.

Have a look at another example where I exposed the state-wise budget details.

Look at the response in the JSON

{
  "name": "Texas",
  "code": "TX",
  "fips": "48",
  "type": "state",
  "population": 28304596,
  "pop_year": 2017,
  "pop_source": "U.S. Census Bureau, 2017 Population Estimate",
  "median_household_income": 56565.0,
  "mhi_year": 2016,
  "mhi_source": "U.S. Census Bureau, 2016 American Community Survey 1-Year Estimates",
  "total_prime_amount": 160742792396.03,
  "total_prime_awards": 276838,
  "total_face_value_loan_amount": 17807026306.5,
  "total_face_value_loan_prime_awards": 45516,
  "award_amount_per_capita": 5679.04,
  "total_outlays": 104316421986.96
}

Conclusion

MCP is powerful when integrated with multiple API’s. The integration vastly reduces inappropriate responses and hallucinations. One can integrate as many API’s as needed. I want you to pause for a moment and think how you can apply this for your use case.

Would you like a demo application for this use case? ๐Ÿ˜Š
๐Ÿค Connect for a 1:1 https://lnkd.in/g6FDTxcM


Discover more from A Streak of Communication

Subscribe to get the latest posts sent to your email.

Discover more from A Streak of Communication

Subscribe now to keep reading and get access to the full archive.

Continue reading