أنظمة MCP الإنتاجية

الاختبار وأفضل الممارسات

5 دقيقة للقراءة

الاختبار الشامل يضمن أن خادم MCP الخاص بك يعمل بشكل صحيح في جميع السيناريوهات.

فئات الاختبار

النوعالغرضالأدوات
الوحدةاختبار الوظائف الفرديةpytest
التكاملاختبار المكونات معاًpytest-asyncio
E2Eاختبارات النظام الكاملMCP Inspector

اختبار وحدة الأدوات

import pytest
from unittest.mock import AsyncMock, patch

from server import WeatherTool

class TestWeatherTool:
    def test_get_definition(self):
        tool = WeatherTool()
        definition = tool.get_tool_definition()

        assert definition["name"] == "get_weather"
        assert "city" in definition["inputSchema"]["properties"]

    def test_validate_valid_input(self):
        tool = WeatherTool()
        assert tool.validate({"city": "London"}) == True

    def test_validate_empty_city(self):
        tool = WeatherTool()
        with pytest.raises(ValueError, match="المدينة مطلوبة"):
            tool.validate({"city": ""})

    @pytest.mark.asyncio
    async def test_execute(self):
        tool = WeatherTool()
        result = await tool.execute({"city": "London"})

        assert "temperature" in result
        assert "condition" in result

اختبار التكامل

import pytest
from httpx import AsyncClient
from server import app

@pytest.fixture
async def client():
    async with AsyncClient(app=app, base_url="http://test") as ac:
        yield ac

@pytest.mark.asyncio
async def test_mcp_endpoint(client):
    response = await client.post("/mcp", json={
        "jsonrpc": "2.0",
        "id": 1,
        "method": "tools/list"
    })

    assert response.status_code == 200
    data = response.json()
    assert "tools" in data

@pytest.mark.asyncio
async def test_tool_call(client):
    response = await client.post("/mcp", json={
        "jsonrpc": "2.0",
        "id": 1,
        "method": "tools/call",
        "params": {
            "name": "get_weather",
            "arguments": {"city": "Tokyo"}
        }
    })

    assert response.status_code == 200
    assert response.json()["result"]

محاكاة الخدمات الخارجية

@pytest.mark.asyncio
async def test_weather_api_failure():
    with patch("server.fetch_weather", new_callable=AsyncMock) as mock:
        mock.side_effect = Exception("API غير متاح")

        tool = WeatherTool()
        with pytest.raises(McpError) as exc:
            await tool.execute({"city": "London"})

        assert "غير متاح" in str(exc.value)

ملخص أفضل الممارسات

الممارسةالفائدة
استخدم متغيرات البيئةالأمان، المرونة
نفذ فحوصات الصحةمراقبة الموثوقية
أضف مهلات الطلبمنع التعليق
سجّل بيانات منظمةتصحيح سهل
نسّخ API الخاص بكالتوافق العكسي
وثّق الأدوات بوضوحاستخدام AI أفضل
اختبر الحالات الحديةمعالجة قوية

قائمة مراجعة الإنتاج

  • جميع الأسرار في متغيرات البيئة
  • نقطة نهاية فحص الصحة منفذة
  • مقاييس Prometheus مكشوفة
  • التسجيل المنظم مكوّن
  • تحديد المعدل مُمَكَّن
  • CORS مكوّن بشكل صحيح
  • SSL/TLS مُمَكَّن
  • معالجة الأخطاء شاملة
  • الاختبارات تمر في CI/CD

تهانينا! لقد أكملت دورة إتقان MCP. :::

اختبار

اختبار الوحدة 5: أنظمة MCP الإنتاجية

خذ الاختبار
هل كان هذا الدرس مفيدًا؟

سجّل الدخول للتقييم

نشرة أسبوعية مجانية

ابقَ على مسار النيرد

بريد واحد أسبوعياً — دورات، مقالات معمّقة، أدوات، وتجارب ذكاء اصطناعي.

بدون إزعاج. إلغاء الاشتراك في أي وقت.