Skip to main content

Semantic Kernel with MCP Examples

· 3 min read

This directory contains the working code examples from the blog post "Building Semantic Kernel Agents with Model Context Protocol (MCP) Plugins in Python".

Prerequisites

Before running these examples, make sure you have the following:

  1. Python 3.8+
  2. Install the required packages:
pip install semantic-kernel mcp
  1. Set up your OpenAI API key as an environment variable:
# On macOS/Linux
export OPENAI_API_KEY=your-api-key-here

# On Windows (Command Prompt)
set OPENAI_API_KEY=your-api-key-here

# On Windows (PowerShell)
$env:OPENAI_API_KEY="your-api-key-here"

Example Files

The examples are organized as follows:

  1. 01_basic_agent.py - A basic Semantic Kernel agent with streaming responses
  2. 02_mcp_server.py - A simple MCP server that provides calculator functions
  3. 03_agent_with_mcp_stdio.py - SK agent that uses the MCP server via stdio transport
  4. 04_mcp_server_sse.py - MCP server that uses Server-Sent Events (SSE) transport
  5. 05_agent_with_mcp_sse.py - SK agent that connects to the SSE-based MCP server

Running the Examples

Basic Agent (No MCP)

Run the basic streaming agent:

python 01_basic_agent.py

MCP via Standard Input/Output

To use the MCP server with stdio transport:

  1. Open a terminal and run the agent:
python 03_agent_with_mcp_stdio.py

This single command will automatically start the MCP server as a subprocess.

MCP via Server-Sent Events (SSE)

To use the MCP server with SSE transport:

  1. First, start the MCP server in one terminal:
python 04_mcp_server_sse.py
  1. Then, in another terminal, run the agent:
python 05_agent_with_mcp_sse.py

Example Prompts

Try these prompts with the Math Assistant:

  • "What is the sum of 3 and 5?"
  • "Can you multiply 6 by 7?"
  • "If I have 20 apples and give away 8, how many do I have left?"
  • "What is 15 divided by 3?"
  • "Calculate 42 minus 17."

Troubleshooting

  • If you get errors about missing modules, make sure you've installed all required packages.
  • If the agent cannot connect to the MCP server in SSE mode, ensure the server is running and listening on the expected port (default is 8000).
  • For function calling issues, ensure you're using a model that supports function calling (e.g., gpt-4o, gpt-3.5-turbo with function calling).
  • If you see connection errors when exiting the application, this is normal and doesn't affect functionality. The examples use async context managers to properly handle connections and cleanup.

Implementation Notes

  • The MCP plugins are used with async context managers (async with MCPSsePlugin(...) as plugin:) to properly handle connections and cleanup.
  • Both server examples use the generic mcp.run(transport="...") method, where the transport can be "stdio" or "sse" depending on the desired communication method.
  • The SSE client connects to the /sse endpoint on the server (e.g., http://localhost:8000/sse).

Further Reading

For more information, check out these resources: